1

これまで、サイズ変更機能の基礎としてhttp://www.html5canvastutorials.com/labs/html5-canvas-drag-and-drop-resize-and-invert-images/の例を使用してきました。ただし、kineticjs (4.3.0+) の新しいリリースでは、ドラッグ用の特別なレイヤーが導入されています。これには、上記のスクリプトの更新も必要です。上記のスクリプトを更新した結果、以下のスクリプトになりました。主な違いは、グループ (以前は同じレイヤー上にあった) を介してアンカーにアクセスするのではなく、ドラッグしたアンカーを直接呼び出すことです。スクリプトは正常に動作しますが、遅延が発生しています。アンカーの動きが遅くなり、画像のサイズが変更されます。誰でもこれを解決する方法を知っていますか? 前もって感謝します。

function update(group, activeAnchor) {
    var topLeft = group.get(".topLeft")[0];
    var topRight = group.get(".topRight")[0];
    var bottomRight = group.get(".bottomRight")[0];
    var bottomLeft = group.get(".bottomLeft")[0];
    var image = group.get(".image")[0];

    switch (activeAnchor.getName()) {
      case "topLeft":
        var topLeft = activeAnchor;
        topRight.attrs.y = activeAnchor.attrs.y;
        bottomLeft.attrs.x = activeAnchor.attrs.x;
        break;
      case "topRight":
        var topRight = activeAnchor;
        topLeft.attrs.y = activeAnchor.attrs.y;
        bottomRight.attrs.x = activeAnchor.attrs.x;
        break;
      case "bottomRight":
        var bottomRight = activeAnchor;
        bottomLeft.attrs.y = activeAnchor.attrs.y;
        topRight.attrs.x = activeAnchor.attrs.x;
        break;
      case "bottomLeft":
        var bottomLeft = activeAnchor;
        bottomRight.attrs.y = activeAnchor.attrs.y;
        topLeft.attrs.x = activeAnchor.attrs.x;
        break;
    } 

    image.setPosition(topLeft.attrs.x, topLeft.attrs.y);

    var height = bottomLeft.attrs.y - topLeft.attrs.y;
    var width = image.getWidth()*height/image.getHeight();

    topRight.attrs.x = topLeft.attrs.x + width;
    topRight.attrs.y = topLeft.attrs.y;
    bottomRight.attrs.x = topLeft.attrs.x + width;
    bottomRight.attrs.y = topLeft.attrs.y + height;

    if(width && height) {
      image.setSize(width, height);
    }
  }
  function addAnchor(group, x, y, name) {
    var stage = group.getStage();
    var layer = group.getLayer();

    var anchor = new Kinetic.Circle({
      x: x,
      y: y,
      stroke: "#666",
      fill: "#ddd",
      strokeWidth: 2,
      radius: 8,
      name: name,
      draggable: true
    });

    anchor.on("dragmove", function() {
      update(group, this);
      layer.draw();
    });
    anchor.on("mousedown touchstart", function() {
      group.setDraggable(false);
      this.moveToTop();
    });
    anchor.on("dragend", function() {
      group.setDraggable(true);
      layer.draw();
    });
    // add hover styling
    anchor.on("mouseover", function() {
      var layer = this.getLayer();
      document.body.style.cursor = "pointer";
      this.setStrokeWidth(4);
      layer.draw();
    });
    anchor.on("mouseout", function() {
      var layer = this.getLayer();
      document.body.style.cursor = "default";
      this.setStrokeWidth(2);
      layer.draw();
    });

    group.add(anchor);
  }
  function loadImages(sources, callback) {
    var images = {};
    var loadedImages = 0;
    var numImages = 0;
    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 initStage(images) {
    var stage = new Kinetic.Stage({
      container: "container",
      width: 578,
      height: 400
    });

    var yodaGroup = new Kinetic.Group({
      x: 100,
      y: 110,
      draggable: true
    });
    var layer = new Kinetic.Layer();

    /*
     * go ahead and add the groups
     * to the layer and the layer to the
     * stage so that the groups have knowledge
     * of its layer and stage
     */

    layer.add(yodaGroup);
    stage.add(layer);

    // darth vader

    // yoda
    var yodaImg = new Kinetic.Rect({
      x: 0,
      y: 0,
      fill: 'blue',
      width: 93,
      height: 104,
      name: "image"
    });

    yodaGroup.add(yodaImg);
    addAnchor(yodaGroup, 0, 0, "topLeft");
    addAnchor(yodaGroup, 93, 0, "topRight");
    addAnchor(yodaGroup, 93, 104, "bottomRight");
    addAnchor(yodaGroup, 0, 104, "bottomLeft");

    yodaGroup.on("dragstart", function() {
      this.moveToTop();
    });

    stage.draw();

  }

$(document).ready(function(){

var sources = {
      darthVader: "http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg",
      yoda: "http://www.html5canvastutorials.com/demos/assets/yoda.jpg"
    };
    loadImages(sources, initStage);
4

1 に答える 1

0

http://jsfiddle.net/fQepn/

私はあなたが提供したチュートリアルを見て、小さな変更を加えました。あなたの問題は新しいドラッグレイヤーに関連しているようです。4.3.1、http: //d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.3.1.jsにアップグレードする必要があります。ここで、新しいドラッグレイヤーの機能をオフにできます。

単に追加します:

  dragOnTop: false

アンカー構成の内部

  var anchor = new Kinetic.Cir......({x: , y: , ... , dragOnTop: false});

これにより、アイテムがドラッグレイヤーに配置されなくなります。

于 2013-01-22T17:35:20.787 に答える