简体   繁体   中英

Displaying one canvas to another

I have a problem displaying one canvas to another. I do everything according to this solution

<script>
    var source = document.getElementById('hiddenCanvas');
    var source_ctx = source.getContext('2d');
    var img = new Image();
    img.onload = function(){
       source.width = img.width;
       source.height = img.height;
       source_ctx.drawImage(img, 0, 0, img.width, img.height);
       }
    img.src = 'icon.jpg';
    var destination = document.getElementById('visibleCanvas');
    var destin_ctx = destination.getContext('2d');
    destin_ctx.drawImage(source, 0, 0);
</script>

Well, first canvas element displays picture correctly, but whatever I do, the second canvas does not want to display a picture.

You are trying to draw the image from source before the image is loaded and the source even have the image.

Move the last draw operation inside the onload handler. Also remember to set the size for destination canvas:

var source = document.getElementById('hiddenCanvas');
var source_ctx = source.getContext('2d');

var destination = document.getElementById('visibleCanvas');
var destin_ctx = destination.getContext('2d');

var img = new Image();
img.onload = function(){
    source.width = img.width;
    source.height = img.height;
    source_ctx.drawImage(img, 0, 0, img.width, img.height);

    destination.width = source.width;
    destination.height = source.height;
    destin_ctx.drawImage(source, 0, 0);
}
img.src = 'icon.jpg';
<script>
function init()
{
    var source = document.getElementById('hiddenCanvas');
    var source_ctx = source.getContext('2d');

    var destination = document.getElementById('visibleCanvas');
    var destin_ctx = destination.getContext('2d');

    var img = new Image();
    img.onload = function(){
        source.width = img.width;
        source.height = img.height;
        source_ctx.drawImage(img, 0, 0, img.width, img.height);

        destin_ctx.drawImage(source, 0, 0);
    }
    img.src = 'arun.jpg';
}
</script>
</head>
<body onload="init();">
<canvas id="hiddenCanvas" />
<canvas id="visibleCanvas" />

Your code is not working because you are trying to access canvas element before it is loaded to dom

The way you've currently structured the code, img.onload is executed after the destin_ctx.drawImage line. That means that your program flow currently looks something like this:

  1. Image is told to start loading
  2. Destination canvas is drawn using (currently blank) source canvas
  3. Image finishes loading
  4. Image's onload executes, causing the source canvas to be drawn to. The destination canvas is NOT updated, because the destin_ctx.drawImage operation is a one-time copy.

What you need to do is move the destin_ctw.drawImage call to a place in the execution flow where you know the source canvas will definitely contain the appropriate contents. In this simple case, moving it to inside the image's onload would work.

Here's a full (but simplified) HTML file that works for me in Chromium, with a changed image url:

<script>
    function load() {
        var source = document.getElementById('hiddenCanvas');
        var source_ctx = source.getContext('2d');
        var destination = document.getElementById('visibleCanvas');
        var destin_ctx = destination.getContext('2d');
        var img = new Image();
        img.onload = function(){
           source.width = img.width;
           source.height = img.height;
           source_ctx.drawImage(img, 0, 0, img.width, img.height);
           destin_ctx.drawImage(source, 0, 0);
        }
        img.src = 'ship360_32.png';
    }
    </script>

    <body onload="load()">
    <canvas id="hiddenCanvas"></canvas>
    <canvas id="visibleCanvas"></canvas>
    </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM