mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
Merge branch 'hitcanvas-optimization2' of https://github.com/pronebird/konva into pronebird-hitcanvas-optimization2
This commit is contained in:
commit
9f7d8e14cf
33
src/Shape.js
33
src/Shape.js
@ -386,48 +386,33 @@
|
||||
var threshold = alphaThreshold || 0,
|
||||
cachedCanvas = this._cache.canvas,
|
||||
sceneCanvas = this._getCachedSceneCanvas(),
|
||||
sceneContext = sceneCanvas.getContext(),
|
||||
hitCanvas = cachedCanvas.hit,
|
||||
hitContext = hitCanvas.getContext(),
|
||||
width = sceneCanvas.getWidth(),
|
||||
height = sceneCanvas.getHeight(),
|
||||
sceneImageData, sceneData, hitImageData, hitData, len, rgbColorKey, i, alpha;
|
||||
hitWidth = hitCanvas.getWidth(),
|
||||
hitHeight = hitCanvas.getHeight(),
|
||||
hitImageData, hitData, len, rgbColorKey, i, alpha;
|
||||
|
||||
hitContext.clear();
|
||||
var ratio = sceneCanvas.pixelRatio;
|
||||
var hitWidth = Math.floor(width / ratio);
|
||||
var hitHeight = Math.floor(height / ratio);
|
||||
hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
|
||||
|
||||
try {
|
||||
// if pixelRatio > 1 we need to redraw scene canvas into temp canvas
|
||||
// with pixelRatio = 1 to have the same dimension as hitCanvas
|
||||
if (ratio !== 1) {
|
||||
var tempCanvas = new Konva.SceneCanvas({
|
||||
width : hitWidth,
|
||||
height : hitHeight,
|
||||
pixelRatio : 1
|
||||
});
|
||||
tempCanvas.getContext().drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
|
||||
sceneImageData = tempCanvas.getContext().getImageData(0, 0, hitWidth, hitHeight);
|
||||
sceneData = sceneImageData.data;
|
||||
} else {
|
||||
sceneImageData = sceneContext.getImageData(0, 0, width, height);
|
||||
sceneData = sceneImageData.data;
|
||||
}
|
||||
hitImageData = hitContext.getImageData(0, 0, hitWidth, hitHeight);
|
||||
hitData = hitImageData.data;
|
||||
len = sceneData.length;
|
||||
len = hitData.length;
|
||||
rgbColorKey = Konva.Util._hexToRgb(this.colorKey);
|
||||
|
||||
// replace non transparent pixels with color key
|
||||
for(i = 0; i < len; i += 4) {
|
||||
alpha = sceneData[i + 3];
|
||||
alpha = hitData[i + 3];
|
||||
if (alpha > threshold) {
|
||||
hitData[i] = rgbColorKey.r;
|
||||
hitData[i + 1] = rgbColorKey.g;
|
||||
hitData[i + 2] = rgbColorKey.b;
|
||||
hitData[i + 3] = 255;
|
||||
}
|
||||
else {
|
||||
hitData[i + 3] = 0;
|
||||
}
|
||||
}
|
||||
hitContext.putImageData(hitImageData, 0, 0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user