-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvasimage.js-e
221 lines (200 loc) · 5.91 KB
/
canvasimage.js-e
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Copyright (c) 2013, Empirical Modelling Group
* All rights reserved.
*
* See LICENSE.txt
*/
#Taints the canvas if the file is located on another domain.
${{
CanvasImage = function (name, dx, dy, dWidth, dHeight, sx1, sy1, sx2, sy2, smoothed, file) {
this.name = edenUI.plugins.Canvas2D.initZoneFromName(name, "Image");
this.obsName = root.currentObservableName();
var temp;
if (sx2 !== undefined) {
if (sx1 > sx2) {
temp = sx1;
sx1 = sx2;
sx2 = temp;
}
this.sWidth = sx2 - sx1;
}
if (sy2 !== undefined) {
if (sy1 > sy2) {
temp = sy1;
sy1 = sy2;
sy2 = temp;
}
this.sHeight = sy2 - sy1;
}
this.originalDWidth = dWidth;
this.originalDHeight = dHeight;
this.originalSX2 = sx2;
this.originalSY2 = sy2;
this.dx = dx;
this.dy = dy;
this.dWidth = dWidth;
this.dHeight = dHeight;
this.sx = sx1;
this.sy = sy1;
this.smoothed = smoothed;
if (CanvasImage.cache[file]) {
this.image = CanvasImage.cache[file];
this.loaded = this.image.complete;
var me = this;
if (this.loaded) {
if (me.sWidth === undefined) {
me.sWidth = me.image.width - me.sx;
}
if (me.sHeight === undefined) {
me.sHeight = me.image.height - me.sy;
}
if (me.dWidth === undefined && me.dHeight === undefined) {
me.dWidth = me.sWidth;
me.dHeight = me.sHeight;
} else if (me.dWidth === undefined) {
me.dWidth = me.sWidth * me.dHeight / me.sHeight;
} else if (me.dHeight === undefined) {
me.dHeight = me.sHeight * me.dWidth / me.sWidth;
}
}
} else {
this.image = new Image();
//this.image.setAttribute("crossorigin","anonymous");
this.canvasesToRepaint = {};
this.loaded = false;
var me = this;
this.image.onload = function() {
if (me.sWidth === undefined) {
me.sWidth = me.image.width - me.sx;
}
if (me.sHeight === undefined) {
me.sHeight = me.image.height - me.sy;
}
if (me.dWidth === undefined && me.dHeight === undefined) {
me.dWidth = me.sWidth;
me.dHeight = me.sHeight;
} else if (me.dWidth === undefined) {
me.dWidth = me.sWidth * me.dHeight / me.sHeight;
} else if (me.dHeight === undefined) {
me.dHeight = me.sHeight * me.dWidth / me.sWidth;
}
me.loaded = true;
for (var viewName in me.canvasesToRepaint) {
edenUI.plugins.Canvas2D.drawPicture(viewName);
}
}
this.image.src = file;
CanvasImage.cache[file] = this.image;
}
};
CanvasImage.cache = {};
}}$;
#! Creates an image element for display on the canvas together with the
# geometric shapes. Taints the canvas if url is located on another domain.
#
# ##Usage Example
# `Image(x, y, url);`
# `Image(x, y, width, height, url);`
#
# ##Examples
# * [Scaling an Image](@external CanvasExamples > scaledimage)
# * [Using OpenClipart](@external CanvasExamples > openclipart)
# * [Rotating an Image](@external CanvasExamples > rotateimage)
# * [Keyframe Animation](@external CanvasExamples > keyframeimage)
# * [Sprites](@external CanvasExamples > sprites)
# * [Upload an Image](@external CanvasExamples > uploadimage)
#
# @param [name]
# @param x X-coordinate
# @param y Y-coordinate
# @param [width] Width in pixels
# @param [height] Height in pixels
# @param [clip_left]
# @param [clip_top]
# @param [clip_right]
# @param [clip_bottom]
# @param [smoothed]
# @param url Source file for the image
#
# #canvas #library #picture #photo #photograph #draw
func Image {
${{
var numArgs = arguments.length;
var argsProcessed;
var name;
if (typeof(arguments[0]) == "string") {
name = arguments[0];
argsProcessed = 1;
} else {
argsProcessed = 0;
}
var dx = arguments[argsProcessed];
argsProcessed++;
var dy = arguments[argsProcessed];
argsProcessed++;
var dWidth, dHeight, sx1 = 0, sy1 = 0, sx2, sy2, smoothed = true, file;
if (numArgs - 1 > argsProcessed) {
dWidth = arguments[argsProcessed];
argsProcessed++;
}
if (numArgs - 1 > argsProcessed) {
dHeight = arguments[argsProcessed];
argsProcessed++;
}
if (numArgs - 1 > argsProcessed) {
sx1 = arguments[argsProcessed];
argsProcessed++;
}
if (numArgs - 1 > argsProcessed) {
sy1 = arguments[argsProcessed];
argsProcessed++;
}
if (numArgs - 1 > argsProcessed) {
sx2 = arguments[argsProcessed];
argsProcessed++;
}
if (numArgs - 1 > argsProcessed) {
sy2 = arguments[argsProcessed];
argsProcessed++;
}
if (numArgs - 1 > argsProcessed) {
smoothed = arguments[argsProcessed];
argsProcessed++;
}
file = arguments[numArgs - 1];
return new CanvasImage(name, dx, dy, dWidth, dHeight, sx1, sy1, sx2, sy2, smoothed, file);
}}$;
}
${{
CanvasImage.prototype.draw = function(context, scale, viewName) {
if (this.loaded) {
var smoothed = this.smoothed;
context.mozImageSmoothingEnabled = smoothed;
context.msImageSmoothingEnabled = smoothed;
context.imageSmoothingEnabled = smoothed;
if (scale > 0) {
context.drawImage(this.image, this.sx, this.sy, this.sWidth, this.sHeight, this.dx, this.dy, this.dWidth, this.dHeight);
} else {
if (!this.flippedImage) {
var buffer = document.createElement("canvas");
buffer.width = this.dWidth;
buffer.height = this.dHeight;
var bufferContext = buffer.getContext("2d");
bufferContext.scale(1, -1);
bufferContext.drawImage(this.image, this.sx, this.sy, this.sWidth, this.sHeight, 0, -this.dHeight, this.dWidth, this.dHeight);
this.flippedImage = buffer;
}
context.drawImage(this.flippedImage, this.dx, this.dy - this.dHeight);
}
} else {
this.canvasesToRepaint[viewName] = true;
}
};
CanvasImage.prototype.isHit = function (context, scale, x, y) {
return x >= this.dx && x < this.dx + this.dWidth && y >= this.dy && y < this.dy + this.dHeight;
}
CanvasImage.prototype.toString = function() {
return "Image(" + Eden.edenCodeForValues(this.dx, this.dy, this.originalDWidth,
this.originalDHeight, this.sx, this.sy, this.originalSX2, this.originalSY2, this.smoothed, this.image.src) + ")";
};
CanvasImage.prototype.getEdenCode = CanvasImage.prototype.toString;
}}$;