mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Better deal with security exceptions and missing image data in Raster.
This commit is contained in:
parent
435341f30b
commit
0954dc3823
1 changed files with 23 additions and 8 deletions
|
@ -42,6 +42,7 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
if (arg0.getContext) {
|
if (arg0.getContext) {
|
||||||
this.setCanvas(arg0);
|
this.setCanvas(arg0);
|
||||||
} else if (typeof arg0 === 'string') {
|
} else if (typeof arg0 === 'string') {
|
||||||
|
// Both data-urls and normal urls are supported here!
|
||||||
this.setSource(arg0);
|
this.setSource(arg0);
|
||||||
} else {
|
} else {
|
||||||
this.setImage(arg0);
|
this.setImage(arg0);
|
||||||
|
@ -153,9 +154,17 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
|
|
||||||
getCanvas: function() {
|
getCanvas: function() {
|
||||||
if (!this._canvas) {
|
if (!this._canvas) {
|
||||||
this._canvas = CanvasProvider.getCanvas(this._size);
|
var canvas = CanvasProvider.getCanvas(this._size);
|
||||||
if (this._image)
|
// Since drawimage images into canvases might fail based on security
|
||||||
this.getContext(true).drawImage(this._image, 0, 0);
|
// policies, wrap the call in try-catch and only set _canvas if we
|
||||||
|
// succeeded.
|
||||||
|
try {
|
||||||
|
if (this._image)
|
||||||
|
this.getContext(true).drawImage(this._image, 0, 0);
|
||||||
|
this._canvas = canvas;
|
||||||
|
} catch (e) {
|
||||||
|
CanvasProvider.returnCanvas(canvas);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this._canvas;
|
return this._canvas;
|
||||||
},
|
},
|
||||||
|
@ -195,7 +204,8 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
},
|
},
|
||||||
|
|
||||||
getSource: function() {
|
getSource: function() {
|
||||||
return this.getImage().src;
|
var img = this.getImage();
|
||||||
|
return img && img.src || null;
|
||||||
},
|
},
|
||||||
|
|
||||||
setSource: function(src) {
|
setSource: function(src) {
|
||||||
|
@ -240,9 +250,13 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
},
|
},
|
||||||
|
|
||||||
toDataURL: function() {
|
toDataURL: function() {
|
||||||
// See if the linked image is base64 encoded already, if so reuse it.
|
// See if the linked image is base64 encoded already, if so reuse it,
|
||||||
|
// otherwise try using canvas.toDataUrl()
|
||||||
var src = this._image && this._image.src;
|
var src = this._image && this._image.src;
|
||||||
return /^data:/.test(src) ? src : this.getCanvas().toDataURL();
|
if (/^data:/.test(src))
|
||||||
|
return src;
|
||||||
|
var canvas = this.getCanvas();
|
||||||
|
return canvas ? canvas.toDataURL() : null;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -442,8 +456,9 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
||||||
},
|
},
|
||||||
|
|
||||||
draw: function(ctx, param) {
|
draw: function(ctx, param) {
|
||||||
ctx.drawImage(this._canvas || this._image,
|
var img = this._canvas || this._image;
|
||||||
-this._size.width / 2, -this._size.height / 2);
|
if (img)
|
||||||
|
ctx.drawImage(img, -this._size.width / 2, -this._size.height / 2);
|
||||||
},
|
},
|
||||||
|
|
||||||
drawSelected: function(ctx, matrix) {
|
drawSelected: function(ctx, matrix) {
|
||||||
|
|
Loading…
Reference in a new issue