forked from digital-flowers/createjs-free-transform-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
172 lines (143 loc) · 4.22 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
var canvas, stage;
var update = true;
var boundary;
var boundaryLine;
var selectTool;
var container;
function init() {
// create stage and point it to the canvas:
canvas = document.getElementById("Designer");
stage = new createjs.Stage(canvas);
// enable touch interactions if supported on the current device:
createjs.Touch.enable(stage);
// enabled mouse over / out events
stage.enableMouseOver(10);
stage.mouseMoveOutside = true; // keep tracking the mouse even when it leaves the canvas
// set up free transform tool
var top = new createjs.Container();
top.name = "top";
stage.addChild(top);
// define boundary
boundary = new createjs.Rectangle();
updateBoundary(boundary);
boundaryLine = new createjs.Shape();
top.addChild(boundaryLine);
let controlsSize = 10 * window.devicePixelRatio;
selectTool = new createjs.util.FreeTransformTool(
"#057",
true,
"rgba(255,255,255,0.8)",
controlsSize,
boundary
);
selectTool.name = "transform";
top.addChild(selectTool);
// load the source image:
var image = new Image();
image.src = "demo/daisy.png";
image.onload = handleImageLoad;
}
function stop() {
createjs.Ticker.removeEventListener("tick", tick);
}
function updateBoundary(boundary) {
var top = canvas.height * 0.1;
var left = canvas.width * 0.1;
var padding = Math.min(top, left);
boundary.setValues(
padding,
padding,
canvas.width - padding * 2,
canvas.height - padding * 2
);
}
function drawBoundary() {
boundaryLine.graphics
.clear()
.beginStroke("rgba(100, 100, 100, 0.5)")
.setStrokeDash([20, 4])
.drawRect(boundary.x, boundary.y, boundary.width, boundary.height);
}
function constrainStageObjects(objects) {
objects.forEach(function(obj) {
createjs.util.constrainObjectTo(obj, boundary);
});
}
function handleImageLoad(event) {
var image = event.target;
var bitmap;
container = new createjs.Container();
stage.addChildAt(container, 0);
// Shape
var ellipse = new createjs.Shape();
ellipse.x = canvas.width / 2;
ellipse.y = canvas.height / 4;
ellipse.setBounds(0, 0, 200, 300);
ellipse.regX = (ellipse.getBounds().width / 2) | 0;
ellipse.regY = (ellipse.getBounds().height / 6) | 0;
ellipse.graphics
.setStrokeStyle(4)
.beginRadialGradientFill(["#FFF", "#35E"], [1, 0], 0, 0, 200, 30, -50, 40)
.drawEllipse(0, 0, 200, 300);
clickToSelect(ellipse);
container.addChild(ellipse);
// Bitmap
bitmap = new createjs.Bitmap(image);
bitmap.x = canvas.width / 2;
bitmap.y = canvas.height / 6;
bitmap.rotation = -25 | 0;
bitmap.regX = (bitmap.image.width / 2) | 0;
bitmap.regY = (bitmap.image.height / 2) | 0;
bitmap.name = "flower";
bitmap.cursor = "pointer";
clickToSelect(bitmap);
container.addChild(bitmap);
// Text
var text = new createjs.Text("Hello\nWorld", "70px Arial", "#052865");
var textBounds = text.getBounds();
text.regX = textBounds.width / 2;
text.regY = textBounds.height / 2;
text.outline = 5;
text.x = canvas.width / 2;
text.y = canvas.height / 2.3;
text.rotation = 5 | 0;
text.cursor = "pointer";
var hit = new createjs.Shape();
hit.graphics
.beginFill("#000")
.drawRect(0, 0, text.getBounds().width, text.getBounds().height);
text.hitArea = hit;
clickToSelect(text);
container.addChild(text);
createjs.Ticker.addEventListener("tick", tick);
handleResize();
}
function clickToSelect(displayObject) {
displayObject.on("click", function(evt) {
evt.stopPropagation();
selectTool.select(evt.currentTarget, stage);
update = true;
});
}
function tick(event) {
// this set makes it so the stage only re-renders when an event handler indicates a change has happened.
if (update) {
update = false; // only update once
stage.update(event);
}
}
var containerElement = document.getElementById("CanvasContainer");
window.addEventListener("resize", handleResize);
function handleResize() {
selectTool.unselect();
var w = containerElement.clientWidth; // -2 accounts for the border
//var h = window.innerHeight-2;
stage.canvas.width = w;
//stage.canvas.height = h;
updateBoundary(boundary);
// TODO: constrain all elements
constrainStageObjects(container.children);
drawBoundary();
stage.update();
}
init();