-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookbook.js
183 lines (145 loc) · 4.73 KB
/
lookbook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class LookBook {
constructor(container) {
this.containerNode = container;
this.lookBookCanvasContainerNode = this.containerNode.querySelector('.js-lookbook-canvas-container');
this.lookBookItemNodes = this.containerNode.querySelectorAll('.js-lookbook-item');
this.downloadButtonNode = this.containerNode.querySelector('.js-lookbook-download');
this.lookBookItems = [];
this.pixiApp = null;
this.init();
}
init() {
this.initCanvas();
this.initLookBookItems();
this.initDownloadButton();
}
initCanvas() {
const requiredCanvasDimensions = this.lookBookCanvasContainerNode.getBoundingClientRect();
this.pixiApp = new PIXI.Application({
width: requiredCanvasDimensions.width,
height: requiredCanvasDimensions.height,
transparent: true,
});
this.lookBookCanvasContainerNode.appendChild(this.pixiApp.view);
}
initLookBookItems() {
this.lookBookItemNodes.forEach(lookBookItemNode => {
const newLookBookItem = {
node: lookBookItemNode,
isSelected: false,
canvasItem: null,
}
this.lookBookItems.push(newLookBookItem)
lookBookItemNode.addEventListener('click', () => {
this.handleLookBookItemClick(newLookBookItem);
});
})
}
initDownloadButton() {
this.downloadButtonNode.addEventListener('click', () => {
this.downloadImage();
})
}
handleLookBookItemClick(lookBookItem) {
lookBookItem.isSelected = !lookBookItem.isSelected;
this.updateItemsState();
}
updateItemsState() {
this.lookBookItems.forEach(async (lookBookItem) => {
const {
node,
isSelected,
canvasItem,
} = lookBookItem;
if (isSelected) {
node.classList.add('is-selected');
if (!canvasItem) {
const newCanvasItem = await this.addItemOnCanvas(node.dataset.image);
lookBookItem.canvasItem = newCanvasItem;
}
} else {
node.classList.remove('is-selected');
if (canvasItem) {
this.pixiApp.stage.removeChild(canvasItem);
lookBookItem.canvasItem = null;
}
}
})
}
async addItemOnCanvas(itemUrl) {
const texture = PIXI.Texture.from(itemUrl, {
resourceOptions: { autoLoad: false }
});
await texture.baseTexture.resource.load();
texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST;
return createCanvasItem(this.pixiApp, texture);
}
downloadImage() {
this.pixiApp.renderer.plugins.extract.canvas(this.pixiApp.stage).toBlob(function (b) {
var a = document.createElement("a");
document.body.append(a);
a.download = 'lookbook';
a.href = URL.createObjectURL(b);
a.click();
a.remove();
}, "image/png");
}
}
function createCanvasItem(app, texture) {
const targetPosition = [app.view.width / 2, app.view.height / 2];
const targetSpriteHeight = app.view.height / 4;
const textureAspectRatio = texture.baseTexture.width / texture.baseTexture.height;
// create our little item...
const canvasItem = new PIXI.Sprite(texture);
canvasItem.height = targetSpriteHeight;
canvasItem.width = canvasItem.height * textureAspectRatio;
// enable the item to be interactive... this will allow it to respond to mouse and touch events
canvasItem.interactive = true;
// this button mode will mean the hand cursor appears when you roll over the item with your mouse
canvasItem.buttonMode = true;
// center the item's anchor point
canvasItem.anchor.set(0.5);
// setup events for mouse + touch using
// the pointer events
canvasItem
.on('pointerdown', onDragStart)
.on('pointerup', onDragEnd)
.on('pointerupoutside', onDragEnd)
.on('pointermove', onDragMove);
// For mouse-only events
// .on('mousedown', onDragStart)
// .on('mouseup', onDragEnd)
// .on('mouseupoutside', onDragEnd)
// .on('mousemove', onDragMove);
// For touch-only events
// .on('touchstart', onDragStart)
// .on('touchend', onDragEnd)
// .on('touchendoutside', onDragEnd)
// .on('touchmove', onDragMove);
// move the sprite to its designated position
canvasItem.x = targetPosition[0];
canvasItem.y = targetPosition[1];
// add it to the stage
app.stage.addChild(canvasItem);
return canvasItem;
}
function onDragStart(event) {
// the reason for this is because of multitouch
// we want to track the movement of this particular touch
this.data = event.data;
this.alpha = 0.8;
this.dragging = true;
}
function onDragEnd() {
this.alpha = 1;
this.dragging = false;
// set the interaction data to null
this.data = null;
}
function onDragMove() {
if (this.dragging) {
const newPosition = this.data.getLocalPosition(this.parent);
this.x = newPosition.x;
this.y = newPosition.y;
}
}