admin管理员组文章数量:1023108
Let say I have x, y, width and height. I need to draw a inclined/tilted Rectangle at particular angle. I cannot use context.rotate. Because it is changing other shapes of canvas.
Let say I have x, y, width and height. I need to draw a inclined/tilted Rectangle at particular angle. I cannot use context.rotate. Because it is changing other shapes of canvas.
Share Improve this question asked Mar 15, 2012 at 11:06 Imran Qadir Baksh - BalochImran Qadir Baksh - Baloch 34.1k69 gold badges195 silver badges348 bronze badges 3- tulrich./geekstuff/canvas/perspective.html – CraigTP Commented Mar 15, 2012 at 11:21
- Can you please show me the code? – Imran Qadir Baksh - Baloch Commented Mar 15, 2012 at 11:26
- 1 You can see the code by viewing the source of the page (HTML) along with the linked jsgl.js file. – CraigTP Commented Mar 15, 2012 at 11:51
1 Answer
Reset to default 3You can use context.rotate
, you just have to undo the rotation before you draw the other shapes. Like this:
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.beginPath();
context.rect(88, 50, 200, 100);
context.fillStyle = "#8ED6FF";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
context.rotate(0.5);
context.beginPath();
context.rect(138, 120, 200, 100);
context.fillStyle = "#FE8E9D";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
context.rotate(-0.5);
context.beginPath();
context.rect(188, 190, 200, 100);
context.fillStyle = "#FEEF8E";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
Let say I have x, y, width and height. I need to draw a inclined/tilted Rectangle at particular angle. I cannot use context.rotate. Because it is changing other shapes of canvas.
Let say I have x, y, width and height. I need to draw a inclined/tilted Rectangle at particular angle. I cannot use context.rotate. Because it is changing other shapes of canvas.
Share Improve this question asked Mar 15, 2012 at 11:06 Imran Qadir Baksh - BalochImran Qadir Baksh - Baloch 34.1k69 gold badges195 silver badges348 bronze badges 3- tulrich./geekstuff/canvas/perspective.html – CraigTP Commented Mar 15, 2012 at 11:21
- Can you please show me the code? – Imran Qadir Baksh - Baloch Commented Mar 15, 2012 at 11:26
- 1 You can see the code by viewing the source of the page (HTML) along with the linked jsgl.js file. – CraigTP Commented Mar 15, 2012 at 11:51
1 Answer
Reset to default 3You can use context.rotate
, you just have to undo the rotation before you draw the other shapes. Like this:
var canvas = document.getElementById("mycanvas");
var context = canvas.getContext("2d");
context.beginPath();
context.rect(88, 50, 200, 100);
context.fillStyle = "#8ED6FF";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
context.rotate(0.5);
context.beginPath();
context.rect(138, 120, 200, 100);
context.fillStyle = "#FE8E9D";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
context.rotate(-0.5);
context.beginPath();
context.rect(188, 190, 200, 100);
context.fillStyle = "#FEEF8E";
context.fill();
context.lineWidth = 5;
context.strokeStyle = "black";
context.stroke();
本文标签: javascriptHow to draw Tilted Rectangle in Html 5 CanvasStack Overflow
版权声明:本文标题:javascript - How to draw Tilted Rectangle in Html 5 Canvas? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745511824a2153859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论