具有可拖动图像的kinetic.js中的复杂剪裁边界

时间:2013-01-17 02:43:44

标签: draggable mask kineticjs clipping

上查看基于html5的剪辑约束

http://shedlimited.debrucellc.com/test3/canvaskinclip.html

(在http://jsfiddle.net/aqaP7/4/上弄乱jsfiddle)

因此,在html5中,我可以轻松地绘制如下形状的边界:

         context.beginPath();
  context.moveTo(5, 5);
  context.lineTo(34, 202);

  context.lineTo(2, 405);
  context.lineTo(212, 385);
  context.lineTo(425, 405);
  context.lineTo(400, 202);
  context.lineTo(415, 10);
  context.lineTo(212, 25);
                context.clip();

在kinetic.js中,我所看到的剪辑选项是:高度,宽度和x,y,

我遇到了以下内容:Mask/Clip an Image using a Polygon in KineticJS,但内部/填充图片无法设置为可拖动

请帮助!

1 个答案:

答案 0 :(得分:2)

在新的kineticJS版本中,很多工作都是在后台完成的。

看看这个tutorial

这个fiddle让你非常接近,这是代码:

 <body>
    <div id="container"></div>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.0-beta2.js"></script>
    <script>
      function loadImages(sources, callback) {
        var images = {};
        var loadedImages = 0;
        var numImages = 0;
        // get num of sources
        for(var src in sources) {
          numImages++;
        }
        for(var src in sources) {
          images[src] = new Image();
          images[src].onload = function() {
            if(++loadedImages >= numImages) {
              callback(images);
            }
          };
          images[src].src = sources[src];
        }
      }
      function draw(images) {
        var stage = new Kinetic.Stage({
          container: 'container',
          width: 600,
          height: 700
        });
        var layer = new Kinetic.Layer();

        var patternPentagon = new Kinetic.RegularPolygon({
          x: 220,
          y: stage.getHeight() / 4,
          sides: 5,
          radius: 70,
          fillPatternImage: images.yoda,
          fillPatternOffset: [-220, 70],
          stroke: 'black',
          strokeWidth: 4,
          draggable: true
        });


        patternPentagon.on('dragmove', function() {
        //this.setFillPatternImage(images.yoda);
          //this.setFillPatternOffset(-100, 70);
          var userPos = stage.getUserPosition();
          this.setFillPatternOffset(-userPos.x,-userPos.y);
          layer.draw();
            this.setX(220);
            this.setY(stage.getHeight() / 4);
        });


        layer.add(patternPentagon);

        stage.add(layer);
      }
      var sources = {
        darthVader: 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg',
        yoda: 'http://www.html5canvastutorials.com/demos/assets/yoda.jpg'
      };

      loadImages(sources, function(images) {
        draw(images);
      });

    </script>
  </body>

有一种更复杂/准确的方法可以做到这一点,而不会将其作为背景图案,就像将对象组合在一起一样