mirror of
https://github.com/konvajs/konva.git
synced 2025-04-05 20:48:28 +08:00
fix bug for drawHitFromCache + HDPI. close #48
This commit is contained in:
parent
4f9140539e
commit
6ad6104f4b
@ -7,7 +7,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
### Fixed
|
### Fixed
|
||||||
- `to` don't throw error if no `onFinish` callback
|
- `to` don't throw error if no `onFinish` callback
|
||||||
- HDPI support for desktop
|
- HDPI support for desktop
|
||||||
- Fix bug when filters on not correct for HDPI
|
- Fix bug when filters are not correct for HDPI
|
||||||
|
- Fix bug when hit area is not correct for HDPI
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- context wrapper is more capable with native context.
|
- context wrapper is more capable with native context.
|
||||||
|
25
src/Shape.js
25
src/Shape.js
@ -394,11 +394,27 @@
|
|||||||
sceneImageData, sceneData, hitImageData, hitData, len, rgbColorKey, i, alpha;
|
sceneImageData, sceneData, hitImageData, hitData, len, rgbColorKey, i, alpha;
|
||||||
|
|
||||||
hitContext.clear();
|
hitContext.clear();
|
||||||
|
var ratio = sceneCanvas.pixelRatio;
|
||||||
|
var hitWidth = Math.floor(width / ratio);
|
||||||
|
var hitHeight = Math.floor(height / ratio);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
sceneImageData = sceneContext.getImageData(0, 0, width, height);
|
// if pixelRatio > 1 we need to redraw scene canvas into temp canvas
|
||||||
sceneData = sceneImageData.data;
|
// with pixelRatio = 1 to have the same dimension as hitCanvas
|
||||||
hitImageData = hitContext.getImageData(0, 0, width, height);
|
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;
|
hitData = hitImageData.data;
|
||||||
len = sceneData.length;
|
len = sceneData.length;
|
||||||
rgbColorKey = Konva.Util._hexToRgb(this.colorKey);
|
rgbColorKey = Konva.Util._hexToRgb(this.colorKey);
|
||||||
@ -413,11 +429,10 @@
|
|||||||
hitData[i + 3] = 255;
|
hitData[i + 3] = 255;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hitContext.putImageData(hitImageData, 0, 0);
|
hitContext.putImageData(hitImageData, 0, 0);
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
Konva.Util.warn('Unable to draw hit graph from cached scene canvas. ' + e.message);
|
Konva.Util.error('Unable to draw hit graph from cached scene canvas. ' + e.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -340,4 +340,48 @@ suite('Manual', function() {
|
|||||||
tween.play();
|
tween.play();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
test.only('create image hit region with pixelRatio, look at hit, test hit with mouseover', function(done) {
|
||||||
|
var imageObj = new Image();
|
||||||
|
|
||||||
|
Konva.pixelRatio = 2;
|
||||||
|
var stage = addStage();
|
||||||
|
var layer = new Konva.Layer();
|
||||||
|
|
||||||
|
imageObj.onload = function() {
|
||||||
|
|
||||||
|
var lion = new Konva.Image({
|
||||||
|
x: 200,
|
||||||
|
y: 40,
|
||||||
|
image: imageObj,
|
||||||
|
draggable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
layer.add(lion);
|
||||||
|
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
lion.cache();
|
||||||
|
lion.drawHitFromCache();
|
||||||
|
layer.draw();
|
||||||
|
|
||||||
|
lion.on('mouseenter', function() {
|
||||||
|
document.body.style.cursor = 'pointer';
|
||||||
|
});
|
||||||
|
|
||||||
|
lion.on('mouseleave', function() {
|
||||||
|
document.body.style.cursor = 'default';
|
||||||
|
});
|
||||||
|
|
||||||
|
Konva.pixelRatio = 1;
|
||||||
|
done();
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
imageObj.src = 'assets/lion.png';
|
||||||
|
|
||||||
|
showHit(layer);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user