javascript - 如何更改 Canvas 元素的位置?

标签 javascript css canvas

我可以在不使用 CSS 的情况下在 Javascript 中更改 Canvas 元素的 x 和 y 位置吗?我似乎找不到任何人甚至在 Internet 上询问它。

编辑:可以用 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>

最佳答案

<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>

关于javascript - 如何更改 Canvas 元素的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9220440/

相关文章:

html - 随着内容扩展 div 宽度

css - 使用 CSS 创建仅显示 Angular 的自定义边框

html - 如何制作即使在小页面和宽度下也能完美的 html 表格

javascript - 选择选项中的 HTML Canvas 函数

css - Cufon 绘制了不必要的空间

javascript - 将 JSON 拆分为多个变量/数组

javascript - 在 javascript 上创建的新对象有多清晰

javascript - php函数在表单内调用

javascript - 使用 image.onload 将多个图像绘制到 Canvas

javascript - 管理界面的 IP 白名单