mirror of
https://github.com/konvajs/konva.git
synced 2025-09-19 19:07:59 +08:00
new graph caching mechanism now supports containers as well. added test
This commit is contained in:
@@ -239,26 +239,35 @@
|
||||
});
|
||||
},
|
||||
drawScene: function(can) {
|
||||
var layer = this.getLayer(),
|
||||
canvas = can || (layer && layer.getCanvas()),
|
||||
cachedCanvas = this._cache.canvas,
|
||||
cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
|
||||
|
||||
if (this.isVisible()) {
|
||||
this._draw(can, 'drawScene');
|
||||
this._draw(canvas, cachedSceneCanvas, 'drawScene');
|
||||
}
|
||||
return this;
|
||||
},
|
||||
drawHit: function(can) {
|
||||
var layer = this.getLayer(),
|
||||
canvas = can || (layer && layer.hitCanvas),
|
||||
cachedCanvas = this._cache.canvas,
|
||||
cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
|
||||
|
||||
if (this.shouldDrawHit()) {
|
||||
this._draw(can, 'drawHit');
|
||||
this._draw(canvas, cachedHitCanvas, 'drawHit');
|
||||
}
|
||||
return this;
|
||||
},
|
||||
_draw: function(can, method) {
|
||||
var clipWidth = this.getClipWidth(),
|
||||
_draw: function(canvas, cachedCanvas, drawMethod) {
|
||||
var context = canvas && canvas.getContext(),
|
||||
clipWidth = this.getClipWidth(),
|
||||
clipHeight = this.getClipHeight(),
|
||||
hasClip = clipWidth && clipHeight,
|
||||
canvas, context, clipX, clipY;
|
||||
context, clipX, clipY;
|
||||
|
||||
if (hasClip) {
|
||||
canvas = can || this.getLayer().hitCanvas;
|
||||
context = canvas.getContext();
|
||||
clipX = this.getClipX();
|
||||
clipY = this.getClipY();
|
||||
|
||||
@@ -270,9 +279,17 @@
|
||||
context.reset();
|
||||
}
|
||||
|
||||
this.children.each(function(child) {
|
||||
child[method](canvas);
|
||||
});
|
||||
if (cachedCanvas) {
|
||||
context.save();
|
||||
context._applyTransform(this);
|
||||
context.drawImage(cachedCanvas._canvas, 0, 0);
|
||||
context.restore();
|
||||
}
|
||||
else {
|
||||
this.children.each(function(child) {
|
||||
child[drawMethod](canvas);
|
||||
});
|
||||
}
|
||||
|
||||
if (hasClip) {
|
||||
context.restore();
|
||||
|
@@ -2990,18 +2990,30 @@ suite('Node', function() {
|
||||
layer.add(group);
|
||||
stage.add(layer);
|
||||
|
||||
//console.log('---before cache')
|
||||
|
||||
assert.equal(group._cache.canvas, undefined);
|
||||
|
||||
group.cache({
|
||||
width: 100,
|
||||
height: 100
|
||||
width: 80,
|
||||
height: 80
|
||||
});
|
||||
|
||||
assert.notEqual(group._cache.canvas.scene, undefined);
|
||||
assert.notEqual(group._cache.canvas.hit, undefined);
|
||||
|
||||
//console.log('---before first draw')
|
||||
layer.draw();
|
||||
|
||||
//console.log(layer.getContext().getTrace())
|
||||
|
||||
//document.body.appendChild(group._cache.canvas.scene._canvas);
|
||||
|
||||
//console.log('---before second draw')
|
||||
layer.draw();
|
||||
|
||||
assert.equal(layer.getContext().getTrace(), 'clearRect(0,0,578,200);save();transform(1,0,0,1,100,30);beginPath();arc(0,0,30,0,6.283,false);closePath();fillStyle=green;fill();lineWidth=4;strokeStyle=black;stroke();restore();save();transform(1,0,0,1,170,100);beginPath();arc(0,0,30,0,6.283,false);closePath();fillStyle=red;fill();lineWidth=4;strokeStyle=black;stroke();restore();save();transform(1,0,0,1,100,170);beginPath();arc(0,0,30,0,6.283,false);closePath();fillStyle=blue;fill();lineWidth=4;strokeStyle=black;stroke();restore();save();transform(1,0,0,1,30,100);beginPath();arc(0,0,30,0,6.283,false);closePath();fillStyle=yellow;fill();lineWidth=4;strokeStyle=black;stroke();restore();clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);drawImage([object HTMLCanvasElement],0,0);restore();clearRect(0,0,578,200);save();transform(1,0,0,1,100,100);drawImage([object HTMLCanvasElement],0,0);restore();');
|
||||
|
||||
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user