不显示在KineticJS中绘制的圆弧

时间:2013-10-28 15:57:49

标签: javascript kineticjs

如果使用KineticJS绘制弧线,请在浏览器控制台中获取错误:“对象#没有方法'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>

我可以不使用画布绘制弧,只需将“context”作为drawFunc函数参数传递:

 drawFunc: function(context) {

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

1 个答案:

答案 0 :(得分:1)

我看到你升级到了Kinetic 4.7.2

以4.7.0开头的版本有一个略有不同的drawFunc,它提供了一个上下文包装器而不是一个canvas包装器。

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);
},