【问题标题】:Drawing a circle using Kinetic Js使用 Kinetic Js 画圆
【发布时间】:2012-11-02 01:29:33
【问题描述】:

我正在尝试使用动态 js 框架构建一个非常基本的绘画应用程序。 一切进展顺利,直到我尝试合并一个形状函数,该函数允许用户单击舞台以指定圆的中心点,拖动并释放鼠标以计算半径,然后将变量传递给构造函数并输出形状。

我的代码似乎运行正常,我将圆形对象打印到控制台,它的变量是我期望的,但由于某种原因它没有出现在我的舞台上!

以下是我的完整代码。关于圆的方法是朝向底部的。谁能看出它为什么没有出现在舞台上?!

 <script src="kinetic-v4.0.4.js"></script>
<script src="utils.js"></script>
<script>

    //variables
    var stage;
    var canvas;
    var context;
    var mouse;
    var startX, startY, endX, endY;
    var rad;
    var dx, dy;

    //run when the page is loaded
    function init(){

        //Create a stage object
        stage = new Kinetic.Stage({
            container: 'canvas',
            height: 400,
            width: 400,
            draggable: true,
            });

        //References
        //get references to elements within the body
        canvas = document.getElementById("canvas");
        context = canvas.getContext("2d");

        //Capture the coordinates of the mouse in relation to the canvas
        mouse = utils.captureMouse(canvas);

        //Add listener to stage.
        stage.getContainer().addEventListener("mousedown", mouseDown, false);
    }

    function mouseDown(){
        console.log("mousedown");
        stage.getContainer().addEventListener("mousemove", mouseMove, false);
        context.beginPath();
    }

    function mouseMove(){
        console.log("mousemove");
        stage.getContainer().addEventListener("mouseup", mouseUp, false);
        context.lineTo(mouse.x, mouse.y);
        context.stroke();
    }

    function mouseUp(){
        console.log("mouseup");
        stage.getContainer().removeEventListener("mousemove", mouseMove, false);
        context.closePath();
    }

    function circle(){
        //add listeners.
        stage.getContainer().addEventListener("mousedown", getCircleStart, false);
    }

    function getCircleStart(){
        startX = mouse.x;
        startY = mouse.y;
        console.log("START POINTS: X: " + startX + " " + "Y: " + startY);
        stage.getContainer().addEventListener("mouseup", getCircleEnd, false);
    }

    function getCircleEnd(){
        endX = mouse.x;
        endY = mouse.y;
        console.log("END POINTS: X: " + endX + " " + "Y: " + endY);
        dx = endX - startX;
        dy = endY - startY;
        console.log(dx + " " + dy);
        rad = Math.pow(dx, 2) + Math.pow(dy, 2);
        rad = Math.sqrt(rad);
        console.log("radius: " + rad);

        //create layer for circle
        var layer = new Kinetic.Layer();

        //draw circle
        var circle = new Kinetic.Circle({
            x: startX,
            y: startY,
            radius: rad,
            fill: 'red',
            stroke: 'black',
            strokeWidth: 4,
            visible: true
            });

        //add to layer
        layer.add(circle);
        //add layer to stage
        stage.add(layer);

        console.log(circle);
    }

utils.js -> http://paulirish.com/2011/requestanimationframe-for-smart-animating/

【问题讨论】:

    标签: javascript html kineticjs


    【解决方案1】:

    尝试添加此代码:circle.getParent().draw() 这可能会解决您的问题。

    【讨论】:

      【解决方案2】:

      JSFiddle 会很好,但乍一看,当您向其添加形状时,您似乎并没有重绘图层。尝试在 getCircleEnd 方法的底部调用 layer.draw()。

      【讨论】:

      • JSFiddle 似乎现在已经关闭,但如果没有其他人在我之前找到解决方案,我会尽快制作一个以确认我的答案!
      • 嘿,我尝试将 .draw() 函数放在您建议的位置,不幸的是它没有改变任何东西。 Circle 对象仍在创建中,只是没有出现在舞台上……我在添加圆圈后添加了控制台的屏幕截图,您可以看到它有一个 x、y 并且它的可见属性设置为 true!我不知道发生了什么!
      • 你能做一个JSFiddle吗?没有你的 utils.js,我什么都做不了。
      • 嘿,我以前从未使用过 jsFiddle,我不知道如何上传外部 js 文件。所以我在上面的帖子中包含了我的 utils.js 代码。我对这一切都很陌生,所以我很抱歉。我从一位名叫 Paul Irish 的开发人员的博客中获得了 utils.js 代码。
      • 不,你应该这样做。那肯定是你的问题的原因。 KineticJS 需要一个普通的 div 用于舞台容器,然后在其中创建自己的画布元素(它为您创建的每个图层创建一个画布元素,然后将它们放在彼此的顶部)。不要担心失去功能,你现在走在正确的轨道上!
      猜你喜欢
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 2012-11-26
      • 2013-09-12
      • 2014-04-29
      • 2014-02-28
      • 1970-01-01
      • 2012-10-19
      相关资源
      最近更新 更多