简体   繁体   中英

How to use an image DOM element as a value for the source property in jCanvas?

I want to test this example : http://calebevans.me/projects/jcanvas/sandbox.php#%24%28%22canvas%22%29.drawImage%28%7B%0A%20%20source%3A%20%24%28%27%23image-fish%27%29%5B0%5D%2C%0A%20%20x%3A%2050%2C%20y%3A%2050%2C%0A%20%20width%3A%20100%2C%0A%20%20fromCenter%3A%20false%0A%7D%29%3B

But I don't see any result in the browser except image by default "img src". Please someone help me. How can I use this function?

  <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Demo</title> 
        <canvas width="500" height="250"></canvas> 
      </head>
        <script src="jquery.js"></script>
        <script src="jcanvas.js"></script>
        <script>

        $("canvas").drawImage({
        source: $('#draggable')[0],
        x: 50, y: 50,
        width: 60,
        fromCenter: false
        });

        </script>            
         <body>
            <img src="../image/0.jpg" class="drag-image" id="draggable"/>
        </div>
      </body>
    </html>

Thanks.

image tag is not fully loaded when source for the canvas tag is set. As a result, $('#draggable')[0] is returning undefined. try calling drawImage on load of the document. The following should fix your problem.

 <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Demo</title> 
        <canvas width="500" height="250"></canvas> 
      </head>
        <script src="jquery.js"></script>
        <script src="jcanvas.js"></script>
        <script>

        $(document).ready(function(){
            $("canvas").drawImage({
                 source: $('#draggable')[0],
                 x: 50, y: 50,
                 width: 60,
                 fromCenter: false
              });
         });

        </script>            
         <body>
            <img src="../image/0.jpg" class="drag-image" id="draggable"/>
        </div>
      </body>
    </html>

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