mirror of
https://github.com/konvajs/konva.git
synced 2025-07-15 18:50:52 +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,
|
var threshold = alphaThreshold || 0,
|
||||||
cachedCanvas = this._cache.canvas,
|
cachedCanvas = this._cache.canvas,
|
||||||
sceneCanvas = this._getCachedSceneCanvas(),
|
sceneCanvas = this._getCachedSceneCanvas(),
|
||||||
sceneContext = sceneCanvas.getContext(),
|
|
||||||
hitCanvas = cachedCanvas.hit,
|
hitCanvas = cachedCanvas.hit,
|
||||||
hitContext = hitCanvas.getContext(),
|
hitContext = hitCanvas.getContext(),
|
||||||
width = sceneCanvas.getWidth(),
|
hitWidth = hitCanvas.getWidth(),
|
||||||
height = sceneCanvas.getHeight(),
|
hitHeight = hitCanvas.getHeight(),
|
||||||
sceneImageData, sceneData, hitImageData, hitData, len, rgbColorKey, i, alpha;
|
hitImageData, hitData, len, rgbColorKey, i, alpha;
|
||||||
|
|
||||||
hitContext.clear();
|
hitContext.clear();
|
||||||
var ratio = sceneCanvas.pixelRatio;
|
hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
|
||||||
var hitWidth = Math.floor(width / ratio);
|
|
||||||
var hitHeight = Math.floor(height / ratio);
|
|
||||||
|
|
||||||
try {
|
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);
|
hitImageData = hitContext.getImageData(0, 0, hitWidth, hitHeight);
|
||||||
hitData = hitImageData.data;
|
hitData = hitImageData.data;
|
||||||
len = sceneData.length;
|
len = hitData.length;
|
||||||
rgbColorKey = Konva.Util._hexToRgb(this.colorKey);
|
rgbColorKey = Konva.Util._hexToRgb(this.colorKey);
|
||||||
|
|
||||||
// replace non transparent pixels with color key
|
// replace non transparent pixels with color key
|
||||||
for(i = 0; i < len; i += 4) {
|
for(i = 0; i < len; i += 4) {
|
||||||
alpha = sceneData[i + 3];
|
alpha = hitData[i + 3];
|
||||||
if (alpha > threshold) {
|
if (alpha > threshold) {
|
||||||
hitData[i] = rgbColorKey.r;
|
hitData[i] = rgbColorKey.r;
|
||||||
hitData[i + 1] = rgbColorKey.g;
|
hitData[i + 1] = rgbColorKey.g;
|
||||||
hitData[i + 2] = rgbColorKey.b;
|
hitData[i + 2] = rgbColorKey.b;
|
||||||
hitData[i + 3] = 255;
|
hitData[i + 3] = 255;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
hitData[i + 3] = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
hitContext.putImageData(hitImageData, 0, 0);
|
hitContext.putImageData(hitImageData, 0, 0);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user