Skip to content

Commit 95f2a4d

Browse files
committed
Use a sprite texture as the background instead of a graphics object
Address #115
1 parent 5a8e0f1 commit 95f2a4d

File tree

3 files changed

+42
-45
lines changed

3 files changed

+42
-45
lines changed

src/image-with-background.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,31 @@ const DEFAULT_BACKGROUND_OPACITY = 0.2;
1717
const DEFAULT_PADDING = 0;
1818

1919
const withBackground = ({
20+
background,
2021
backgroundColor,
21-
backgroundGraphics,
2222
backgroundOpacity
2323
}) => self =>
2424
assign(self, {
2525
get backgroundColor() {
26-
return backgroundGraphics.fill.color;
26+
return background.tint;
27+
},
28+
setBackgroundColor(color = backgroundColor) {
29+
background.tint = color;
2730
},
2831
get backgroundOpacity() {
29-
return backgroundGraphics.fill.alpha;
32+
return background.alpha;
3033
},
31-
clearBackground() {
32-
backgroundGraphics.clear();
34+
setBackgroundOpacity(opacity = backgroundOpacity) {
35+
background.alpha = opacity;
3336
},
34-
drawBackground(
35-
color = backgroundColor,
36-
opacity = backgroundOpacity,
37-
withPadding = false
38-
) {
37+
rescaleBackground(withPadding = false) {
3938
const width = self.width + self.padding * withPadding;
4039
const height = self.height + self.padding * withPadding;
4140

42-
backgroundGraphics.clear();
43-
backgroundGraphics.beginFill(color, opacity);
44-
backgroundGraphics.drawRect(-width / 2, -height / 2, width, height);
45-
backgroundGraphics.endFill();
41+
background.x = -width / 2;
42+
background.y = -height / 2;
43+
background.width = width;
44+
background.height = height;
4645
}
4746
});
4847

@@ -67,7 +66,8 @@ const createImageWithBackground = (
6766
padding = DEFAULT_PADDING
6867
} = {}
6968
) => {
70-
const backgroundGraphics = new PIXI.Graphics();
69+
const container = new PIXI.Container();
70+
const background = new PIXI.Sprite(PIXI.Texture.WHITE);
7171
const displayObject = toDisplayObject(source);
7272

7373
let sprite;
@@ -79,29 +79,24 @@ const createImageWithBackground = (
7979
sprite = displayObject;
8080
}
8181

82-
const init = self => {
83-
backgroundGraphics.addChild(sprite);
84-
85-
return self;
86-
};
82+
container.addChild(background);
83+
container.addChild(sprite);
8784

88-
return init(
89-
pipe(
90-
withStaticProperty('displayObject', backgroundGraphics),
91-
withStaticProperty('sprite', sprite),
92-
withColorFilters(sprite),
93-
withScale(sprite, displayObject.width, displayObject.height),
94-
withSize(sprite, displayObject.width, displayObject.height),
95-
withPadding(padding),
96-
withBackground({
97-
backgroundColor,
98-
backgroundGraphics,
99-
backgroundOpacity
100-
}),
101-
withDestroy(backgroundGraphics),
102-
withConstructor(createImageWithBackground)
103-
)({})
104-
);
85+
return pipe(
86+
withStaticProperty('displayObject', container),
87+
withStaticProperty('sprite', sprite),
88+
withColorFilters(sprite),
89+
withScale(sprite, displayObject.width, displayObject.height),
90+
withSize(sprite, displayObject.width, displayObject.height),
91+
withPadding(padding),
92+
withBackground({
93+
background,
94+
backgroundColor,
95+
backgroundOpacity
96+
}),
97+
withDestroy(sprite),
98+
withConstructor(createImageWithBackground)
99+
)({});
105100
};
106101

107102
export default createImageWithBackground;

src/library.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ const createPilingJs = (rootElement, initOptions = {}) => {
877877

878878
item.preview.scaleX(xScale);
879879
item.preview.scaleY(yScale);
880-
item.preview.drawBackground();
880+
item.preview.rescaleBackground();
881881
}
882882
});
883883
});

src/pile.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,9 @@ const createPile = (
164164
index
165165
);
166166

167-
item.image.drawBackground(
168-
getForegroundColor(borderColor),
169-
borderOpacity,
170-
true
171-
);
167+
item.image.setBackgroundColor(getForegroundColor(borderColor));
168+
item.image.setBackgroundOpacity(borderOpacity);
169+
item.image.rescaleBackground(true);
172170
hoverPreviewContainer.addChild(item.displayObject);
173171
}
174172
render();
@@ -210,11 +208,15 @@ const createPile = (
210208
previewBackgroundColor === INHERIT
211209
? pileBackgroundColor
212210
: getItemProp(previewBackgroundColor, items[item.id], index);
211+
213212
const backgroundOpacity =
214213
previewBackgroundOpacity === INHERIT
215214
? getPileProp(pileBackgroundOpacity, piles[id])
216215
: getItemProp(previewBackgroundOpacity, items[item.id], index);
217-
item.image.drawBackground(backgroundColor, backgroundOpacity);
216+
217+
item.image.setBackgroundColor(backgroundColor);
218+
item.image.setBackgroundOpacity(backgroundOpacity);
219+
218220
previewItemContainer.addChild(item.displayObject);
219221
}
220222
render();
@@ -839,7 +841,7 @@ const createPile = (
839841
prevSize = [item.preview.width, item.preview.height];
840842
}
841843

842-
item.preview.clearBackground();
844+
item.preview.setBackgroundOpacity(0);
843845

844846
animatePositionItems(
845847
previewItem,

0 commit comments

Comments
 (0)