在畫布繪畫應用程序中,我想添加要素以在整個畫布上創建疊加層,然後當我在畫布上製作特定矩形時,疊加層應從該區域中移除,就像https://onpaste.com/(選擇焦點工具)上的一樣。我的想法是,如果在畫布上製作一個矩形,那麼我可以裁剪當前圖像,然後將圖像粘貼到選定位置的覆蓋圖上。我不確定,如何在將圖像粘貼到畫布上之前裁剪圖像,我嘗試使用Kinetic.Image對象的方法setFillPaternImage,但在此我想要提供Kinetic.Image對象而不是javascript image對象,因爲在Kinetic.Image對象上我可以使用setAttrs方法。 請告訴我如何裁剪和添加圖像,或者是否有更好的方法來實現。鏈接撥弄 - >http://jsfiddle.net/hearsid/9a2Hn/使用kineticjs在畫布上創建疊加層並刪除其中的部分
<html>
<head>
<style>
</style>
</head>
<body>
<div id="container"></div>
<button id="button">this</button>
<script src="js/jquery.js"></script>
<script src="js/kinetic.js"></script>
<script>
var stage = new Kinetic.Stage({
container:'container',
width:500,
height:300
});
var layer=new Kinetic.Layer();
var img = new Image();
img.onload = function() {
imgObj = new Kinetic.Image({
x:0,y:0,width:400,height:300,image:img
});
layer.add(imgObj);
var circle = new Kinetic.Circle({
x:30,y:30,radius:30,fill:'red'
});
layer.add(circle);
var rect = new Kinetic.Rect({
x:0,y:0,width:300,height:500,fill:'gray',opacity:0.5
});
layer.add(rect);
stage.add(layer);
}
img.src="some.jpg";
$("#button").click(function() {
rect2 = new Kinetic.Rect({
x:200,y:30,width:100,height:100
});
/*
Careful with the commented area , here I wanted to crop the image before using FillPaternImage
var img2 = new Image();
img2.onload = function() {
imgObj2 = new Kinetic.Image({
image: img2 ,
x:300
});
imgObj2 = imgObj.clone();
imgObj2.setAttrs({image :img2 ,x:100 , y:0 });
*/
img2 = img ;
rect2.setFillPatternImage(img2);
layer.add(rect2);
layer.draw();
});
</script>
</body>
</html>