kineticjs - mask/contain images so no overlap

孤人 提交于 2019-12-06 15:25:43

You can use a Kinetic Group plus “yoda cropping” to be sure your images don’t overlap

First create a Group that will contain both the picture frame and the Yoda:

If you need to drag or scale, you would drag or scale the Group (all contained elements would react accordingly)

    var group=new Kinetic.Group({
        x:20,
        y:20,
        width:256,
        height:256,
        draggable:true
    })
    layer.add(group);

Then add Yoda image which has been cropped to fit in the picture frame.

Since this Yoda image in in the background (lower z-index), any slight overlap with the picture frame will be hidden by the larger picture frame.

Here, the Yoda would be bigger than the picture frame, so it needs to be cropped a bit.

        var inner=new Kinetic.Image({
            image:Yoda,
            x:44,
            y:44,
            width:168,
            height:162,
            crop:{
                x: 23,
                y: 38,
                width: 168,
                height: 162
            }

        });
        group.add(inner);

Finally, add the picture frame which will be scaled to fit in the group using width/height:

        var outer=new Kinetic.Image({
            image:pictureFrame,
            x:0,
            y:0,
            width:256,
            height:256,
        });
        group.add(outer); 

Here is code and a Fiddle: http://jsfiddle.net/m1erickson/qGHZn/

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>

<style>
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:400px;
  height:400px;
}
</style>        
<script>
$(function(){

    var stage = new Kinetic.Stage({
        container: 'container',
        width: 400,
        height: 400
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);

    var group=new Kinetic.Group({
        x:20,
        y:20,
        width:256,
        height:256,
        draggable:true
    })
    layer.add(group);

    //////////////////////////
    // START
    //////////////////////////


var frame=new Image();
frame.onload=function(){

    var pic=new Image();
    pic.onload=function(){

        var inner=new Kinetic.Image({
            image:pic,
            x:44,
            y:44,
            width:168,
            height:162,
            crop:{
                x: 23,
                y: 38,
                width: 168,
                height: 162
            }

        });
        group.add(inner);

        var outer=new Kinetic.Image({
            image:frame,
            x:0,
            y:0,
            width:256,
            height:256,
        });
        group.add(outer);

        layer.draw();
    }
    pic.src="http://www.html5canvastutorials.com/demos/assets/yoda.jpg";

}
frame.src="woodenframe.png";


}); // end $(function(){});

</script>       
</head>

<body>
    <div id="container"></div>
</body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!