最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I change the location of a canvas element? - Stack Overflow

programmeradmin1浏览0评论

Can I change the x and y position of a canvas element in Javascript without using CSS? I cannot seem to find anyone who even asks it on the Internet.

EDIT: Can it be done in HTML?

<html>
<title>Testing</title>
    <canvas id="main" width="200" height="200" style="border:1px solid     #c3c3c3;">
    Error: Browser does not support canvas element.
    </canvas>
    <script type="text/javascript">         
        var main = document.getElementById("main");
        var render = main.getContext("2d");
        main.width = 200;
        main.height = 200;
        main.style.left = 100x;
        main.style.top = 100px;
    </script>
</html>

Can I change the x and y position of a canvas element in Javascript without using CSS? I cannot seem to find anyone who even asks it on the Internet.

EDIT: Can it be done in HTML?

<html>
<title>Testing</title>
    <canvas id="main" width="200" height="200" style="border:1px solid     #c3c3c3;">
    Error: Browser does not support canvas element.
    </canvas>
    <script type="text/javascript">         
        var main = document.getElementById("main");
        var render = main.getContext("2d");
        main.width = 200;
        main.height = 200;
        main.style.left = 100x;
        main.style.top = 100px;
    </script>
</html>
Share Improve this question edited Feb 9, 2012 at 23:41 Phrogz 303k113 gold badges667 silver badges756 bronze badges asked Feb 9, 2012 at 23:15 MrDrProfessorTylerMrDrProfessorTyler 4232 gold badges10 silver badges26 bronze badges 9
  • Perhaps describe what you are trying to achieve and why. That will allow a better quality of answer. – Jivings Commented Feb 9, 2012 at 23:17
  • 1 I want to set the position (x, y) of the canvas element in the window in Google Chrome. Is there any code I could use to do this? – MrDrProfessorTyler Commented Feb 9, 2012 at 23:17
  • 1 Do you mean without using top and left css properties? By the way why don't you want to use css? – Andrei Schneider Commented Feb 9, 2012 at 23:18
  • Because I want to know if it is possible to be done in Javascript alone. – MrDrProfessorTyler Commented Feb 9, 2012 at 23:19
  • Yes it is, but it serves no purpose. JavaScript is not for aligning your DOM elements. That's why we have CSS. – Jivings Commented Feb 9, 2012 at 23:20
 |  Show 4 more comments

2 Answers 2

Reset to default 11
<html>
<title>Testing</title>
    <canvas id="main" width="200" height="200" style="border:1px solid     #c3c3c3;">
    Error: Browser does not support canvas element.
    </canvas>
    <script type="text/javascript">

        var main = document.getElementById("main");
        var render = main.getContext("2d");
        main.width = 200;
        main.height = 200;
        main.style.left = "100px";
        main.style.top = "100px";
        main.style.position = "absolute";
    </script>
</html>

CSS is used to manage the properties that, among other things, position elements on the page. If you choose not to define a top or left property in the CSS file or in an inline style, you can still set those properties after the canvas is loaded in the DOM using Javascript. However, all you'd be doing with the script is directly altering the same properties that CSS already manages.

There is no technical advantage to using JS to alter properties in lieu of a stylesheet or inline style, unless you're changing it dynamically after the page has already loaded content with the default styles. If you're looking to do animation, check out jQuery for a really nice, cross-browser solution for DOM animation. There is already an answer posted for the basic code to set position once using JS, which as I said is a role best reserved for CSS.

发布评论

评论列表(0)

  1. 暂无评论