Dynamically take Width and height of canvas image and also create another canvas using javascript on one page

huangapple go评论74阅读模式
英文:

Dynamically take Width and height of canvas image and also create another canvas using javascript on one page

问题

我正在使用JavaScript创建一个带有动态图像的画布。现在我想要根据图像动态获取画布的高度和宽度。并且在同一页上创建另一个画布。这里是我的代码。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        var _puzzleWidth = 640;
        var _puzzleHeight = 480;
        var _stage;
        function init(){

            var canvas = document.createElement('canvas');
            canvas.id = "CursorLayer";
            //canvas.width = _puzzleWidth;
            //canvas.height = _puzzleHeight;
            _stage = canvas.getContext('2d');
            canvas.style.border = "1px solid";
            //draw();
            var img = new Image();
            img.onload = function(){
                _stage.drawImage(img, 0, 0);
            }
            img.src = "http://lorempixel.com/output/animals-q-g-640-480-3.jpg";

            var body = document.getElementsByTagName("body")[0];
            body.appendChild(canvas);
        }
    </script>
</head>
<body onload="init();"></body>
</html>

提前感谢。

英文:

I am creating a canvas using javascript with a dynamic image. Now I want to take canvas's height and width dynamically depending on the image. And as well as create another canvas on same page. Here I am attaching my code.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;script&gt;
        var _puzzleWidth = 640;
        var _puzzleHeight = 480;
        var _stage;
        function init(){

            var canvas = document.createElement(&#39;canvas&#39;);
            canvas.id = &quot;CursorLayer&quot;;
            //canvas.width = _puzzleWidth;
            //canvas.height = _puzzleHeight;
            _stage = canvas.getContext(&#39;2d&#39;);
            canvas.style.border = &quot;1px solid&quot;;
            //draw();
            var img = new Image();
            img.onload = function(){
                _stage.drawImage(img, 0, 0);
            }
            img.src = &quot;http://lorempixel.com/output/animals-q-g-640-480-3.jpg&quot;;

            var body = document.getElementsByTagName(&quot;body&quot;)[0];
            body.appendChild(canvas);
        }
        
    &lt;/script&gt;
&lt;/head&gt;
&lt;body onload=&quot;init();&quot;&gt;&lt;/body&gt; 
&lt;/html&gt;

<!-- end snippet -->

Thanks in advance.

答案1

得分: 1

请尝试以下代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        var _puzzleWidth = 640;
        var _puzzleHeight = 480;
        var _stage;
        var _stage2;
        function init(){

            var canvas2 = document.createElement('canvas');
            canvas2.id = "CursorLayer2";

            var canvas = document.createElement('canvas');
            canvas.id = "CursorLayer";

            _stage = canvas.getContext('2d');
            canvas.style.border = "1px solid";

            _stage2 = canvas2.getContext('2d');
            canvas2.style.border = "1px solid";

            var img = new Image();
            var img1 = new Image();

            img.onload = function(){
                canvas.width  = this.width;
                canvas.height = this.height;
                _stage.drawImage(img, 0, 0);
            }

            img1.onload = function(){
                canvas2.width  = this.width;
                canvas2.height = this.height;
                _stage2.drawImage(img1,0,0);
            }

            img.src = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTfSV9AGQi9c48oRysYiuSs9Bs4J3B2p4R3eh-z1hsS-Z01HD17";
            img1.src = "http://lorempixel.com/output/animals-q-g-640-480-3.jpg";

            var body = document.getElementsByTagName("body")[0];
            body.appendChild(canvas);

            var body1 = document.getElementsByTagName("body")[0];
            body1.appendChild(canvas2);
        }
    </script>
</head>
<body onload="init();"></body>
</html>

希望这对你有帮助。

英文:

Try this below code:

 &lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
var _puzzleWidth = 640;
var _puzzleHeight = 480;
var _stage;
var _stage2;
function init(){
var canvas2 = document.createElement(&#39;canvas&#39;);
canvas2.id = &quot;CursorLayer2&quot;;
var canvas = document.createElement(&#39;canvas&#39;);
canvas.id = &quot;CursorLayer&quot;;
_stage = canvas.getContext(&#39;2d&#39;);
canvas.style.border = &quot;1px solid&quot;;
_stage2 = canvas2.getContext(&#39;2d&#39;);
canvas2.style.border = &quot;1px solid&quot;;
var img = new Image();
var img1=new Image();
img.onload = function(){
canvas.width  = this.width;
canvas.height = this.height;
_stage.drawImage(img, 0, 0);
}
img1.onload = function(){
canvas2.width  = this.width;
canvas2.height = this.height;
_stage2.drawImage(img1,0,0);
}
img.src = &quot;https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTfSV9AGQi9c48oRysYiuSs9Bs4J3B2p4R3eh-z1hsS-Z01HD17&quot;;
img1.src = &quot;http://lorempixel.com/output/animals-q-g-640-480-3.jpg&quot;;
var body = document.getElementsByTagName(&quot;body&quot;)[0];
body.appendChild(canvas);
var body1 = document.getElementsByTagName(&quot;body&quot;)[0];
body1.appendChild(canvas2);
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload=&quot;init();&quot;&gt;&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2020年1月3日 14:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定