【问题标题】:Javascript click wont fire until after two clicksJavascript 点击不会触发,直到两次点击
【发布时间】:2014-03-02 05:03:57
【问题描述】:

我正在使用动力学 js 并使用单击事件在单击时突出显示元素。我遇到了一个问题,即在第二次点击之前事件不会触发。

function Canvas(){
  this.stage;
  this.backgroundLayer;
}

Canvas.prototype.init = function(w, h){
  this.stage = new Kinetic.Stage({
    container: 'container',
    width: w,
    height: h
  });

  this.backgroundLayer = new Kinetic.Layer();
  this.stage.add(this.backgroundLayer);
  this.addLayerListeners();
}

Canvas.prototype.addLayerListeners = function(){
  this.backgroundLayer.on('click',function(evt){
    var shape = evt.targetNode;
    shape.stroke('#00ff00');
    shape.strokeWidth('5');
  });
}

这是正常行为吗?否则,我做错了什么?

【问题讨论】:

  • @GameAlchemist:在 KineticJS 中 stroke() 实际上并没有绘制笔画。它将设置strokeStyle。所以 Jordy 只是为后续绘制设置 strokeStyle。 :)

标签: javascript html canvas kineticjs


【解决方案1】:

代码肯定比我看到的要多。 例如,在事件监听器中,你必须在某个地方有一个 shape.draw()、layer.draw() 或 stage.draw()。

我在 onclick(选择)函数中使用了类似的方法:

shape.setStroke('black');
shape.setStrokeWidth(4);
shape.enableStroke();
shape.draw();

并在删除笔划(取消选择)功能中:

shape.disableStroke();
shape.draw();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多