forked from jedateach/pixijs-free-transform-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
176 lines (146 loc) · 4.11 KB
/
demo.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
(function(){
var canvas, app, stage, container;
var boundary;
var boundaryLine;
var selectTool;
// Resize function window
function resize() {
selectTool.unselect();
const parent = app.view.parentNode;
app.renderer.resize(parent.clientWidth, app.renderer.height);
updateBoundary(boundary);
constrainStageObjects(container.children, boundary);
}
function updateBoundary(boundary) {
var top = canvas.height * 0.1;
var left = canvas.width * 0.1;
var padding = Math.min(top, left);
boundary.x = padding;
boundary.y = padding;
boundary.width = canvas.width - padding * 2;
boundary.height = canvas.height - padding * 2;
drawBoundaryLine();
}
function constrainStageObjects(objects, container) {
objects.forEach(function(obj) {
PIXI.util.constrainObjectTo(obj, container);
});
}
function drawBoundaryLine() {
boundaryLine.clear();
boundaryLine.lineWidth = 1;
boundaryLine.lineColor = "#000";
boundaryLine
.lineStyle(1, 0x000000, 1)
.drawRect(boundary.x, boundary.y, boundary.width, boundary.height);
}
function clickToSelect(obj) {
obj.interactive = true;
obj.cursor = "pointer";
obj.on("pointertap", function (evt) {
evt.stopPropagation();
selectTool.select(evt.currentTarget);
});
}
function init() {
canvas = document.getElementById("Designer");
app = new PIXI.Application({
width: 800, height: 600,
transparent: true,
antialias: true,
resolution: 1,
view: canvas
});
stage = app.stage;
// Listen for window resize events
window.addEventListener('resize', resize);
// set up free transform tool
const top = new PIXI.Container();
// top.name = "top";
app.stage.addChild(top);
// define boundary
boundary = new PIXI.Rectangle();
boundaryLine = new PIXI.Graphics();
top.addChild(boundaryLine);
updateBoundary(boundary);
let controlsSize = 10 * window.devicePixelRatio;
selectTool = new PIXI.util.FreeTransformTool(0x005577, true, null, controlsSize, boundary);
selectTool.name = "transform";
top.addChild(selectTool);
container = new PIXI.Container();
stage.addChildAt(container, 0);
// app.renderer.plugins.interaction.on("pointerup", function(e) {
// console.log("click ", e.data.global.x, e.data.global.y);
// });
let ellipse = createEllipse();
container.addChild(ellipse);
let image = createBitmap('demo/flower.svg');
container.addChild(image);
let text = createText("Off\nCentre");
container.addChild(text);
// TODO: figure out how to animate handle dragging?
// app.ticker.add(function(delta) {
// text.rotation -= 0.01 * delta;
// text.position.x += delta + 1;
// ellipse.rotation += 0.05 * delta;
// ellipse.position.x += delta + (Math.random() - 0.5) * 10;
// ellipse.position.y += delta + (Math.random() - 0.5) * 10;
// image.rotation += 0.03 * delta;
// resize();
// });
resize();
}
function createEllipse() {
// Shape
var ellipse = new PIXI.Graphics();
ellipse.x = canvas.width / 4;
ellipse.y = canvas.height / 4;
ellipse
.beginFill(0x3355EE)
.lineStyle(10, 0x000)
.drawEllipse(0, 0, 50, 100);
// TODO: figure out pivot
// ellipse.pivot.x = ellipse.width * 0.1;
// ellipse.pivot.y = ellipse.height * 0.1;
clickToSelect(ellipse);
return ellipse;
}
function createBitmap(filename) {
// Bitmap
const texture = PIXI.Texture.from(filename);
const bitmap = new PIXI.Sprite(texture);
bitmap.position.set(canvas.width / 2, canvas.height / 6);
bitmap.anchor.set(0.5);
bitmap.rotation = -25 | 0;
clickToSelect(bitmap);
return bitmap;
}
function createText(value) {
let style = new PIXI.TextStyle({
align: "center",
fontFamily: "Century Gothic",
fontSize: 86,
fontWeight: "bold",
fill: ["navy", "black"],
fillGradientType: PIXI.TEXT_GRADIENT.LINEAR_VERTICAL,
// fillGradientStops:
stroke: '#fff',
strokeThickness: 6,
dropShadow: true,
dropShadowColor: "#000000",
dropShadowAlpha: 0.4,
dropShadowBlur: 4,
dropShadowAngle: Math.PI / 6,
dropShadowDistance: 6,
lineJoin: "round"
});
var text = new PIXI.Text(value, style);
text.position.set(canvas.width, canvas.height / 2.3);
text.rotation = 0;
// text.scale.set(1);
text.anchor.set(0.2); // TODO: allow non-centered anchor
clickToSelect(text);
return text;
}
init();
}());