dongzhizhai4070 2012-06-04 06:25 采纳率: 0%
浏览 123
已采纳

在Javascript / Jquery中使用canvas显示图像

I have the following code :

function createImage(source) {                
var pastedImage = new Image();                
pastedImage.onload = function() {                                        
document.write('<br><br><br>Image: <img src="'+pastedImage.src+'" height="700" width="700"/>');                                                                                                         
}
pastedImage.src = source;                
}

Here I am displaying the image through html image tag which I wrote in document.write and provide appropriate height and width to image.

My question is can it possible to displaying image into the canvas instead of html img tag. So that I can drag and crop that image as I want.

But how can I display it in canvas?

Further I want to implement save that image using PHP but for now let me know about previous issue.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • drkj41932 2012-06-04 06:59
    关注

    Try This

     var ctx = document.getElementById('canvas').getContext('2d');
    
        var img = new Image();   // Create new img element  
        img.onload = function(){  
          // execute drawImage statements here  This is essential as it waits till image is loaded before drawing it.
     ctx.drawImage(img , 0, 0);
    
        };  
        img.src = 'myImage.png'; // Set source path  
    

    Make sure the image is hosted in same domain as your site. Read this for Javascript Security Restrictions Same Origin Policy.

    E.g. If your site is http://example.com/ then the Image should be hosted on http://example.com/../myImage.png

    if you try http://facebook.com/..image/ or something then it will throw security error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?