|
3 | 3 | * @module CrysyanCanvas
|
4 | 4 | * @depend util.js
|
5 | 5 | */
|
6 |
| -(function($util) { |
| 6 | +(function ($util) { |
7 | 7 | 'use strict';
|
8 | 8 | /**
|
9 | 9 | * @class CrysyanCanvas
|
|
50 | 50 | // the most length of history 'revokeImgDatas' list
|
51 | 51 | this.historyListLen = ops.historyListLen;
|
52 | 52 | }
|
| 53 | + |
53 | 54 | CrysyanCanvas.prototype = {
|
54 | 55 | // Save e drawing surface
|
55 |
| - saveDrawingSurface: function() { |
| 56 | + saveDrawingSurface: function () { |
56 | 57 | this.drawingSurfaceImageData = this.playContext.getImageData(0, 0,
|
57 | 58 | this.playCanvas.width,
|
58 | 59 | this.playCanvas.height);
|
59 | 60 | },
|
60 | 61 | // Restore drawing surface
|
61 |
| - restoreDrawingSurface: function() { |
| 62 | + restoreDrawingSurface: function () { |
62 | 63 | this.playContext.putImageData(this.drawingSurfaceImageData, 0, 0);
|
63 | 64 | },
|
64 | 65 | // save history ''drawingSurfaceImageData''
|
65 |
| - saveRevokeImgDatas: function() { |
| 66 | + saveRevokeImgDatas: function () { |
66 | 67 | var drawingSurfaceImageData = this.playContext.getImageData(0, 0, this.playCanvas.width, this.playCanvas.height);
|
67 | 68 | if (this.revokeImgDatas.length >= this.historyListLen) {
|
68 | 69 | // If the length is longer than the maximum length of the configuration
|
|
76 | 77 | // clear the array
|
77 | 78 | this.forwardRevokeImgDatas = [];
|
78 | 79 | },
|
79 |
| - saveForwardRevokeFirstFrame: function() { |
| 80 | + saveForwardRevokeFirstFrame: function () { |
80 | 81 | this.forwardRevokeImgDatas[0] = this.playContext.getImageData(0, 0, this.playCanvas.width, this.playCanvas.height);
|
81 | 82 | },
|
82 | 83 | // Revoke
|
83 | 84 | // if length of list is zero ,return zero
|
84 |
| - revoke: function() { |
| 85 | + revoke: function () { |
85 | 86 | if (this.revokeImgDatas.length <= 0)
|
86 | 87 | return 0;
|
87 | 88 | var drawingSurfaceImageData = this.revokeImgDatas.pop();
|
|
90 | 91 | },
|
91 | 92 | // Forward revoke
|
92 | 93 | // if length of list is zero ,return zero
|
93 |
| - forwardRevoke: function() { |
| 94 | + forwardRevoke: function () { |
94 | 95 | if (this.forwardRevokeImgDatas.length <= 0)
|
95 | 96 | return 0;
|
96 | 97 | var drawingSurfaceImageData = this.forwardRevokeImgDatas.pop();
|
97 | 98 | this.revokeImgDatas.push(drawingSurfaceImageData);
|
98 | 99 | this.playContext.putImageData(drawingSurfaceImageData, 0, 0);
|
99 | 100 | },
|
100 | 101 | //
|
101 |
| - clearCanvas: function() { |
| 102 | + clearCanvas: function () { |
102 | 103 | this.playContext.clearRect(0, 0, this.playCanvas.width, this.playCanvas.height);
|
| 104 | + this.clearCanvasWithOnlyBackGroupImage(); |
| 105 | + }, |
| 106 | + // |
| 107 | + clearCanvasWithOnlyBackGroupImage: function () { |
103 | 108 | if (this.backgroudImage.image !== null) {
|
104 | 109 | var image = this.backgroudImage.image;
|
105 | 110 | this.drawImage(image, (this.playCanvas.width - image.width) / 2, (this.playCanvas.height - image.height) / 2, image.width, image.height);
|
|
112 | 117 | * @param {number} x e.clientX
|
113 | 118 | * @param {number} y e.clientY
|
114 | 119 | */
|
115 |
| - windowToCanvas: function(x, y) { |
| 120 | + windowToCanvas: function (x, y) { |
116 | 121 | var bbox = this.playCanvas.getBoundingClientRect();
|
117 | 122 | return {
|
118 | 123 | x: x - bbox.left,
|
|
128 | 133 | * @param {String|File|Image|Blob} obj
|
129 | 134 | * @param mode
|
130 | 135 | */
|
131 |
| - drawBackGroupWithImage: function(obj, mode) { |
| 136 | + drawBackGroupWithImage: function (obj, mode) { |
132 | 137 | if (typeof mode === "undefined") {
|
133 | 138 | // image scaling mode
|
134 | 139 | // if mode !=1 ,fulling mode
|
|
150 | 155 | if (image.width >= canvas.playCanvas.width && ivwr > ivhr) {
|
151 | 156 | // Beyond the canvas's width
|
152 | 157 | // zoom ratio
|
153 |
| - canvas.backgroudImage.height = canvas.playCanvas.height * (1 / ivwr).toFixed(2); |
| 158 | + image.width = canvas.backgroudImage.width; |
| 159 | + image.height = canvas.backgroudImage.height = canvas.playCanvas.height * (1 / ivwr).toFixed(2); |
154 | 160 | } else if (image.height >= canvas.playCanvas.height && (ivhr > ivwr)) {
|
155 | 161 | // Beyond the canvas's height
|
156 | 162 | // zoom ratio
|
157 |
| - canvas.backgroudImage.width = canvas.playCanvas.width * (1 / ivhr).toFixed(2); |
| 163 | + image.height = canvas.backgroudImage.height; |
| 164 | + image.width = canvas.backgroudImage.width = canvas.playCanvas.width * (1 / ivhr).toFixed(2); |
158 | 165 | }
|
159 |
| - image.width = canvas.backgroudImage.width; |
160 |
| - image.height = canvas.backgroudImage.height; |
161 |
| - }else{ |
| 166 | + } else { |
162 | 167 | // if the width of image bigger than canvas's,will set to canvas's width
|
163 | 168 | if (image.width >= canvas.backgroudImage.width) {
|
164 | 169 | image.width = canvas.backgroudImage.width;
|
|
171 | 176 | canvas.clearCanvas();
|
172 | 177 | };
|
173 | 178 | // image the image object maybe come from parent's window.
|
174 |
| - if (obj instanceof Image ||(parent&&parent.window&&obj instanceof parent.window.Image)||typeof obj.src!=="undefined") { |
| 179 | + if (obj instanceof Image || (parent && parent.window && obj instanceof parent.window.Image) || typeof obj.src !== "undefined") { |
175 | 180 | image.src = obj.src;
|
176 | 181 | return;
|
177 | 182 | }
|
178 | 183 | // file the File|Blob object maybe come from parent's window.
|
179 | 184 | if (obj instanceof File
|
180 | 185 | || obj instanceof Blob
|
181 |
| - ||(parent&&parent.window&&(obj instanceof parent.window.File||obj instanceof parent.window.Blob))) { |
| 186 | + || (parent && parent.window && (obj instanceof parent.window.File || obj instanceof parent.window.Blob))) { |
182 | 187 | var reader = new FileReader();
|
183 |
| - reader.onload = function(event) { |
| 188 | + reader.onload = function (event) { |
184 | 189 | image.src = event.target.result;
|
185 | 190 | };
|
186 | 191 | reader.readAsDataURL(file);
|
|
205 | 210 | * 3、Cut the image and locate the part on the canvas:
|
206 | 211 | * drawImageFile(imagefile,sx,sy,swidth,sheight,x,y,width,height);
|
207 | 212 | */
|
208 |
| - drawImageFile: function(file) { |
| 213 | + drawImageFile: function (file) { |
209 | 214 | var canvas = this;
|
210 | 215 | var reader = new FileReader();
|
211 |
| - reader.onload = function(event) { |
| 216 | + reader.onload = function (event) { |
212 | 217 | canvas.drawDataUrl(event.target.result);
|
213 | 218 | };
|
214 | 219 | reader.readAsDataURL(file);
|
|
218 | 223 | *
|
219 | 224 | * @param dataUrl
|
220 | 225 | */
|
221 |
| - drawDataUrl: function(dataUrl) { |
| 226 | + drawDataUrl: function (dataUrl) { |
222 | 227 | var ctx = this.playContext;
|
223 | 228 | var image = new Image();
|
224 | 229 | // CORS settings attributes
|
225 | 230 | image.crossOrigin = 'Anonymous';
|
226 |
| - image.onload = function() { |
| 231 | + image.onload = function () { |
227 | 232 | arguments[0] = image;
|
228 | 233 | ctx.drawImage.apply(ctx, arguments);
|
229 | 234 | };
|
|
241 | 246 | * 3、Cut the image and locate the part on the canvas:
|
242 | 247 | * drawImage(image,sx,sy,swidth,sheight,x,y,width,height);
|
243 | 248 | */
|
244 |
| - drawImage: function() { |
| 249 | + drawImage: function () { |
245 | 250 | var ctx = this.playContext;
|
246 | 251 | ctx.drawImage.apply(ctx, arguments);
|
247 | 252 | },
|
|
255 | 260 | * Other arguments are ignored.
|
256 | 261 | *@return {string}
|
257 | 262 | */
|
258 |
| - toDataURL: function(type, encoderOptions) { |
| 263 | + toDataURL: function (type, encoderOptions) { |
259 | 264 | return this.playCanvas.toDataURL(type, encoderOptions);
|
260 | 265 | },
|
261 | 266 |
|
|
267 | 272 | * @param callback called in image.onload
|
268 | 273 | * @returns {Image} image dom element
|
269 | 274 | */
|
270 |
| - toImageEle: function(type, encoderOptions, callback) { |
| 275 | + toImageEle: function (type, encoderOptions, callback) { |
271 | 276 | var image = new Image();
|
272 |
| - image.onload = function() { |
| 277 | + image.onload = function () { |
273 | 278 | callback();
|
274 | 279 | };
|
275 | 280 | image.src = this.toDataURL(type, encoderOptions);
|
|
280 | 285 | * See toDataURL()
|
281 | 286 | * @return {Blob}
|
282 | 287 | */
|
283 |
| - toBlob: function(type, encoderOptions) { |
| 288 | + toBlob: function (type, encoderOptions) { |
284 | 289 | //DataURL: 'data:text/plain;base64,YWFhYWFhYQ=='
|
285 | 290 | var arr = this.toDataURL(type, encoderOptions).split(','),
|
286 | 291 | mime = arr[0].match(/:(.*?);/)[1],
|
|
300 | 305 | * @deprecated
|
301 | 306 | * @link http://weworkweplay.com/play/saving-html5-canvas-as-image/
|
302 | 307 | */
|
303 |
| - saveAsLocalImagePng: function() { |
| 308 | + saveAsLocalImagePng: function () { |
304 | 309 | // here is the most important part because if you don't replace you will get a DOM 18 exception.
|
305 | 310 | var image = this.toDataURL("image/png").replace("image/png", "image/octet-stream;Content-Disposition:attachment;filename=foo.png");
|
306 | 311 | //var image = this.toDataURL("image/png").replace("image/png", "image/octet-stream");
|
|
309 | 314 | },
|
310 | 315 |
|
311 | 316 | // add event to canvas
|
312 |
| - addEvent: function(eventType, callback) { |
| 317 | + addEvent: function (eventType, callback) { |
313 | 318 | $util.addEvent(this.playCanvas, eventType, callback);
|
314 | 319 | },
|
315 | 320 | //
|
|
0 commit comments