0

如果使用 KineticJS 绘制圆弧,在浏览器控制台中会出现错误:“Object # has no method 'getContext'” 代码:

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
   </head>
  <body>   
  <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.2.min.js"></script>
  <script defer="defer">
      var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 200
      });
      var layer = new Kinetic.Layer();

     var myarc = new Kinetic.Shape({
      drawFunc: function(canvas) {
        var context=canvas.getContext('2d');
        var x = stage.getWidth()/2;
        var y = stage.getHeight()/2;
        var radius = 70;
        var startAngle = 1 * Math.PI;
        var endAngle = 0 * Math.PI;
        context.beginPath();
        context.arc(x, y, radius, startAngle, endAngle, false);
        context.stroke(this);
        canvas.fillStroke(this);
    },
    fill: '#00D2FF',
    stroke: 'red',
    strokeWidth: 15,
    draggable:true
    }); 


     myarc.on('mouseover', function() {
        this.setStroke('blue');
        this.setStrokeWidth(20);
        layer.draw();
      });

      myarc.on('mouseout', function() {
        this.setStroke('black');
        this.setStrokeWidth(4);
        layer.draw();
      });
      // add the shape to the layer
      layer.add(myarc);

      // add the layer to the stage
      stage.add(layer); 
    </script>

    <div id="container"></div>
  </body>
</html>

我可以在不使用画布的情况下绘制弧线,只需将“上下文”作为 drawFunc 函数参数传输:

 drawFunc: function(context) {

,但在这种情况下,我无法在鼠标悬停事件中更改弧的宽度。

4

1 回答 1

1

我看到你已经升级到 Kinetic 4.7.2

从 4.7.0 开始的版本有一个稍微不同的 drawFunc,它提供上下文包装器而不是画布包装器。

https://github.com/ericdrowell/KineticJS/wiki/Change-Log

尝试这个:

  drawFunc: function(context) {
    var x = stage.getWidth()/2;
    var y = stage.getHeight()/2;
    var radius = 70;
    var startAngle = 1 * Math.PI;
    var endAngle = 0 * Math.PI;
    context.beginPath();
    context.arc(x, y, radius, startAngle, endAngle, false);
    context.fillStrokeShape(this);
},
于 2013-10-28T16:23:08.400 回答