mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 14:10:14 -05:00
Merge remote branch 'origin/master'
This commit is contained in:
commit
0c5595b2a7
8 changed files with 68 additions and 71 deletions
|
@ -452,8 +452,8 @@ var Matrix = this.Matrix = Base.extend({
|
|||
/**
|
||||
* Applies this matrix to the specified Canvas Context.
|
||||
*/
|
||||
applyToContext: function(context, reset) {
|
||||
context[reset ? 'setTransform' : 'transform'](
|
||||
applyToContext: function(ctx, reset) {
|
||||
ctx[reset ? 'setTransform' : 'transform'](
|
||||
this._m00, this._m10, this._m01,
|
||||
this._m11, this._m02, this._m12
|
||||
);
|
||||
|
|
|
@ -84,8 +84,8 @@ var Document = this.Document = Base.extend({
|
|||
|
||||
draw: function() {
|
||||
if (this.canvas) {
|
||||
var context = this.context;
|
||||
context.save();
|
||||
var ctx = this.context;
|
||||
ctx.save();
|
||||
|
||||
var testDirtyRects = false;
|
||||
if (testDirtyRects) {
|
||||
|
@ -93,49 +93,48 @@ var Document = this.Document = Base.extend({
|
|||
top = this.size.height / 8;
|
||||
|
||||
function clear(rect) {
|
||||
context.clearRect(rect.x, rect.y, rect.width, rect.height);
|
||||
ctx.clearRect(rect.x, rect.y, rect.width, rect.height);
|
||||
|
||||
if (true) {
|
||||
context.moveTo(rect.x, rect.y);
|
||||
context.lineTo(rect.x + rect.width, rect.y);
|
||||
context.lineTo(rect.x + rect.width, rect.y + rect.height);
|
||||
context.lineTo(rect.x, rect.y + rect.height);
|
||||
ctx.moveTo(rect.x, rect.y);
|
||||
ctx.lineTo(rect.x + rect.width, rect.y);
|
||||
ctx.lineTo(rect.x + rect.width, rect.y + rect.height);
|
||||
ctx.lineTo(rect.x, rect.y + rect.height);
|
||||
}
|
||||
}
|
||||
|
||||
context.beginPath();
|
||||
ctx.beginPath();
|
||||
|
||||
clear(Rectangle.create(left, top, 2 * left, 2 * top));
|
||||
clear(Rectangle.create(3 * left, 3 * top, 2 * left, 2 * top));
|
||||
|
||||
// clear(Rectangle.create(left, top, 4 * left, 4 * top));
|
||||
|
||||
context.closePath();
|
||||
context.clip();
|
||||
ctx.closePath();
|
||||
ctx.clip();
|
||||
} else {
|
||||
// Initial tests conclude that clearing the canvas using clearRect
|
||||
// is always faster than setting canvas.width = canvas.width
|
||||
// http://jsperf.com/clearrect-vs-setting-width/7
|
||||
context.clearRect(0, 0,
|
||||
this.size.width + 1, this.size.height + 1);
|
||||
ctx.clearRect(0, 0, this.size.width + 1, this.size.height + 1);
|
||||
}
|
||||
|
||||
var param = { offset: new Point(0, 0) };
|
||||
for (var i = 0, l = this.layers.length; i < l; i++)
|
||||
Item.draw(this.layers[i], context, param);
|
||||
context.restore();
|
||||
Item.draw(this.layers[i], ctx, param);
|
||||
ctx.restore();
|
||||
|
||||
// Draw the selection of the selected items in the document:
|
||||
if (this._selectedItemCount > 0) {
|
||||
context.save();
|
||||
context.strokeWidth = 1;
|
||||
ctx.save();
|
||||
ctx.strokeWidth = 1;
|
||||
// TODO: use Layer#color
|
||||
context.strokeStyle = context.fillStyle = '#4f7aff';
|
||||
ctx.strokeStyle = ctx.fillStyle = '#4f7aff';
|
||||
param = { selection: true };
|
||||
Base.each(this._selectedItems, function(item) {
|
||||
item.draw(context, param);
|
||||
item.draw(ctx, param);
|
||||
});
|
||||
context.restore();
|
||||
ctx.restore();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -483,10 +483,10 @@ var Item = this.Item = Base.extend({
|
|||
var bounds = this.getStrokeBounds();
|
||||
var scale = resolution / 72;
|
||||
var canvas = CanvasProvider.getCanvas(bounds.getSize().multiply(scale));
|
||||
var context = canvas.getContext('2d');
|
||||
var ctx = canvas.getContext('2d');
|
||||
var matrix = new Matrix().scale(scale).translate(-bounds.x, -bounds.y);
|
||||
matrix.applyToContext(context);
|
||||
this.draw(context, {});
|
||||
matrix.applyToContext(ctx);
|
||||
this.draw(ctx, {});
|
||||
var raster = new Raster(canvas);
|
||||
raster.setPosition(this.getBounds().getCenter());
|
||||
raster.scale(1 / scale);
|
||||
|
@ -653,7 +653,7 @@ var Item = this.Item = Base.extend({
|
|||
// TODO: toString
|
||||
|
||||
statics: {
|
||||
drawSelectedBounds: function(bounds, context, matrix) {
|
||||
drawSelectedBounds: function(bounds, ctx, matrix) {
|
||||
var top = bounds.y,
|
||||
bottom = bounds.y + bounds.height,
|
||||
left = bounds.x,
|
||||
|
@ -666,30 +666,30 @@ var Item = this.Item = Base.extend({
|
|||
];
|
||||
if (matrix)
|
||||
matrix.transform(coords, 0, coords, 0, 4);
|
||||
context.beginPath();
|
||||
ctx.beginPath();
|
||||
for (var i = 0; i < 8; i++)
|
||||
context[i == 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
|
||||
context.closePath();
|
||||
context.stroke();
|
||||
ctx[i == 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]);
|
||||
ctx.closePath();
|
||||
ctx.stroke();
|
||||
for (var i = 0; i < 8; i++) {
|
||||
context.beginPath();
|
||||
context.rect(
|
||||
ctx.beginPath();
|
||||
ctx.rect(
|
||||
Math.round(coords[i]) - 2,
|
||||
Math.round(coords[++i]) - 2,
|
||||
4, 4
|
||||
);
|
||||
context.fill();
|
||||
ctx.fill();
|
||||
}
|
||||
},
|
||||
|
||||
// TODO: Implement DocumentView into the drawing
|
||||
// TODO: Optimize temporary canvas drawing to ignore parts that are
|
||||
// outside of the visible view.
|
||||
draw: function(item, context, param) {
|
||||
draw: function(item, ctx, param) {
|
||||
if (!item.visible || item.opacity == 0)
|
||||
return;
|
||||
|
||||
var tempCanvas, parentContext;
|
||||
var tempCanvas, parentCtx;
|
||||
// If the item has a blendMode or is defining an opacity, draw it on
|
||||
// a temporary canvas first and composite the canvas afterwards.
|
||||
// Paths with an opacity < 1 that both define a fillColor
|
||||
|
@ -711,23 +711,23 @@ var Item = this.Item = Base.extend({
|
|||
tempCanvas = CanvasProvider.getCanvas(size);
|
||||
|
||||
// Save the parent context, so we can draw onto it later
|
||||
parentContext = context;
|
||||
parentCtx = ctx;
|
||||
|
||||
// Set context to the context of the temporary canvas,
|
||||
// so we draw onto it, instead of the parentContext
|
||||
context = tempCanvas.getContext('2d');
|
||||
context.save();
|
||||
// Set ctx to the context of the temporary canvas,
|
||||
// so we draw onto it, instead of the parentCtx
|
||||
ctx = tempCanvas.getContext('2d');
|
||||
ctx.save();
|
||||
|
||||
// Translate the context so the topLeft of the item is at (0, 0)
|
||||
// on the temporary canvas.
|
||||
context.translate(-itemOffset.x, -itemOffset.y);
|
||||
ctx.translate(-itemOffset.x, -itemOffset.y);
|
||||
}
|
||||
var savedOffset;
|
||||
if (itemOffset) {
|
||||
savedOffset = param.offset;
|
||||
param.offset = itemOffset;
|
||||
}
|
||||
item.draw(context, param);
|
||||
item.draw(ctx, param);
|
||||
if (itemOffset)
|
||||
param.offset = savedOffset;
|
||||
|
||||
|
@ -737,7 +737,7 @@ var Item = this.Item = Base.extend({
|
|||
|
||||
// Restore the temporary canvas to its state before the
|
||||
// translation matrix was applied above.
|
||||
context.restore();
|
||||
ctx.restore();
|
||||
|
||||
// If the item has a blendMode, use BlendMode#process to
|
||||
// composite its canvas on the parentCanvas.
|
||||
|
@ -745,16 +745,16 @@ var Item = this.Item = Base.extend({
|
|||
// The pixel offset of the temporary canvas to the parent
|
||||
// canvas.
|
||||
var pixelOffset = itemOffset.subtract(param.offset);
|
||||
BlendMode.process(item.blendMode, context, parentContext,
|
||||
BlendMode.process(item.blendMode, ctx, parentCtx,
|
||||
item.opacity, pixelOffset);
|
||||
} else {
|
||||
// Otherwise we just need to set the globalAlpha before drawing
|
||||
// the temporary canvas on the parent canvas.
|
||||
parentContext.save();
|
||||
parentContext.globalAlpha = item.opacity;
|
||||
parentContext.drawImage(tempCanvas,
|
||||
parentCtx.save();
|
||||
parentCtx.globalAlpha = item.opacity;
|
||||
parentCtx.drawImage(tempCanvas,
|
||||
itemOffset.x, itemOffset.y);
|
||||
parentContext.restore();
|
||||
parentCtx.restore();
|
||||
}
|
||||
|
||||
// Return the temporary canvas, so it can be reused
|
||||
|
|
|
@ -124,9 +124,8 @@ var Raster = this.Raster = Item.extend({
|
|||
|
||||
getSubImage: function(rect) {
|
||||
rect = Rectangle.read(arguments);
|
||||
var canvas = CanvasProvider.getCanvas(rect.getSize()),
|
||||
context = canvas.getContext('2d');
|
||||
context.drawImage(this.getCanvas(), rect.x, rect.y,
|
||||
var canvas = CanvasProvider.getCanvas(rect.getSize());
|
||||
canvas.getContext('2d').drawImage(this.getCanvas(), rect.x, rect.y,
|
||||
canvas.width, canvas.height, 0, 0, canvas.width, canvas.height);
|
||||
return canvas;
|
||||
},
|
||||
|
@ -145,8 +144,7 @@ var Raster = this.Raster = Item.extend({
|
|||
*/
|
||||
getPixel: function(point) {
|
||||
point = Point.read(arguments);
|
||||
var ctx = this.getContext(),
|
||||
pixels = ctx.getImageData(point.x, point.y, 1, 1).data,
|
||||
var pixels = this.getContext().getImageData(point.x, point.y, 1, 1).data,
|
||||
channels = new Array(4);
|
||||
for (var i = 0; i < 4; i++)
|
||||
channels[i] = pixels[i] / 255;
|
||||
|
@ -174,15 +172,15 @@ var Raster = this.Raster = Item.extend({
|
|||
|
||||
getData: function(rect) {
|
||||
rect = Rectangle.read(arguments);
|
||||
var ctx = this.getContext();
|
||||
rect = rect.isEmpty() ? new Rectangle(this.getSize()) : rect;
|
||||
return ctx.getImageData(rect.x, rect.y, rect.width, rect.height);
|
||||
if (rect.isEmpty())
|
||||
rect = new Rectangle(this.getSize());
|
||||
return this.getContext().getImageData(rect.x, rect.y,
|
||||
rect.width, rect.height);
|
||||
},
|
||||
|
||||
setData: function(data, point) {
|
||||
point = Point.read(arguments, 1);
|
||||
var ctx = this.getContext();
|
||||
ctx.putImageData(data, point.x, point.y);
|
||||
this.getContext().putImageData(data, point.x, point.y);
|
||||
},
|
||||
|
||||
_transform: function(matrix, flags) {
|
||||
|
|
|
@ -366,13 +366,13 @@ var Curve = this.Curve = Base.extend({
|
|||
p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y),
|
||||
// Get length of total range
|
||||
rangeLength = Numerical.integrate(ds, a, b,
|
||||
getIterations(a, b)),
|
||||
// Use length / rangeLength for an initial guess for t, to
|
||||
// bring us closer:
|
||||
guess = length / rangeLength,
|
||||
len = 0;
|
||||
getIterations(a, b));
|
||||
if (length >= rangeLength)
|
||||
return forward ? b : a;
|
||||
// Use length / rangeLength for an initial guess for t, to
|
||||
// bring us closer:
|
||||
var guess = length / rangeLength,
|
||||
len = 0;
|
||||
// Iteratively calculate curve range lengths, and add them up,
|
||||
// using integration precision depending on the size of the
|
||||
// range. This is much faster and also more precise than not
|
||||
|
|
|
@ -563,11 +563,11 @@ var Path = this.Path = PathItem.extend({
|
|||
}
|
||||
},
|
||||
|
||||
setContextStyles: function(context) {
|
||||
setContextStyles: function(ctx) {
|
||||
for (var i in styles) {
|
||||
var style;
|
||||
if (style = this[i]()) {
|
||||
context[styles[i]] = style;
|
||||
ctx[styles[i]] = style;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,9 +31,9 @@ var CanvasProvider = {
|
|||
canvas.width = size.width;
|
||||
canvas.height = size.height;
|
||||
} else {
|
||||
var context = canvas.getContext('2d');
|
||||
// +1 is needed on some browsers to really clear the borders
|
||||
context.clearRect(0, 0, size.width + 1, size.height + 1);
|
||||
canvas.getContext('2d').clearRect(0, 0,
|
||||
size.width + 1, size.height + 1);
|
||||
}
|
||||
return canvas;
|
||||
} else {
|
||||
|
|
|
@ -147,14 +147,14 @@ var PaperScript = new function() {
|
|||
try {
|
||||
var onFrame = eval('onFrame');
|
||||
if (onFrame) {
|
||||
function loop() {
|
||||
function frame() {
|
||||
// Request next frame already
|
||||
Event.requestAnimationFrame(loop, doc && doc.canvas);
|
||||
Event.requestAnimationFrame(frame, doc && doc.canvas);
|
||||
onFrame();
|
||||
// Automatically redraw document each frame.
|
||||
doc && doc.redraw();
|
||||
}
|
||||
Event.requestAnimationFrame(loop, doc && doc.canvas);
|
||||
Event.requestAnimationFrame(frame, doc && doc.canvas);
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
|
@ -167,8 +167,8 @@ var PaperScript = new function() {
|
|||
//#ifdef BROWSER
|
||||
// Code borrowed from Coffee Script:
|
||||
function request(url) {
|
||||
var xhr = new (window.ActiveXObject
|
||||
|| XMLHttpRequest)('Microsoft.XMLHTTP');
|
||||
var xhr = new (window.ActiveXObject || XMLHttpRequest)(
|
||||
'Microsoft.XMLHTTP');
|
||||
xhr.open('GET', url, true);
|
||||
if ('overrideMimeType' in xhr) {
|
||||
xhr.overrideMimeType('text/plain');
|
||||
|
|
Loading…
Reference in a new issue