Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML画布-从特定点旋转线条_Javascript_Html_Canvas_Rotation_Html5 Canvas - Fatal编程技术网

Javascript HTML画布-从特定点旋转线条

Javascript HTML画布-从特定点旋转线条,javascript,html,canvas,rotation,html5-canvas,Javascript,Html,Canvas,Rotation,Html5 Canvas,我在HTML画布上的特定点(250250)旋转一行时遇到问题,我想知道人们是如何做到的。我查看了其他答案并尝试了它们,但它们并没有解决我的问题 var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); function circles(){ context.beginPath(); context.

我在HTML画布上的特定点(250250)旋转一行时遇到问题,我想知道人们是如何做到的。我查看了其他答案并尝试了它们,但它们并没有解决我的问题

var canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");

        function circles(){

            context.beginPath();
            context.arc(250,250,6.35,0,2*Math.PI);
            context.stroke();

        }

        function lines(){
            var deg = 45;
            context.beginPath();
            context.moveTo(250,250);
            context.lineTo(250,0);
            context.stroke();

            context.save();
            context.rotate(deg * Math.PI / 180);
            context.translate(20,-25);

    // This is the line I want to rotate
            context.beginPath();
            context.moveTo(250,250);
            context.lineTo(250,0);
            context.stroke();

            //

            context.restore();

            context.beginPath();
            context.moveTo(0,250);
            context.lineTo(500,250);
            context.stroke();

        }

        circles();
        lines();

您已经掌握了上面的代码。关键是,在调用
rotate
之前,需要
将上下文转换为要旋转的点。您还需要
将上下文翻译回末尾

示例及注释如下:

var canvas=document.getElementById(“canvas”);
var context=canvas.getContext(“2d”);
函数圆(){
context.beginPath();
弧(250250225.5,0,2*Math.PI);
stroke();
context.beginPath();
弧(250250170,0,2*Math.PI);
stroke();
context.beginPath();
弧(250250162,0,2*Math.PI);
stroke();
context.beginPath();
弧(250250107,0,2*Math.PI);
stroke();
context.beginPath();
弧(250250,99,0,2*Math.PI);
stroke();
context.beginPath();
弧(250250,16,0,2*Math.PI);
stroke();
context.beginPath();
弧(250250,6.35,0,2*Math.PI);
stroke();
}
功能行(){
var-deg=45;
context.beginPath();
上下文。移动到(250250);
lineTo(250,0);
stroke();
context.save();
//将上下文转换到要旋转的点
语境。翻译(250250);
//执行旋转
旋转角度(deg*Math.PI/180);
//划清界限
context.beginPath();
context.moveTo(0,0);
lineTo(-250,0);
stroke();
//将上下文翻译回其原始位置
翻译(-250,-250);
restore();
context.beginPath();
上下文。移动到(0250);
lineTo(500250);
stroke();
}
//参考线
函数refLines(){
context.beginPath();
上下文。移动到(0250);
lineTo(500250);
stroke();
context.beginPath();
上下文。移动到(250,0);
lineTo(250500);
stroke();
context.beginPath();
context.moveTo(0,0);
lineTo(500500);
stroke();
context.beginPath();
上下文。移动到(0500);
lineTo(500,0);
stroke();
}
圆圈();
行()
部分{
文本对齐:居中;
保证金:自动;
}
帆布{
边框:1px纯黑;
}