简体   繁体   English

如何在链接单击时将图像加载到画布中

[英]How to load image into canvas on link click

I have multiple links with img src. 我与img src有多个链接。 I want to load each image into the canvas when each link clicked. 单击每个链接时,我想将每个图像加载到canvas But it's not work. 但这是行不通的。 Here's the script: 这是脚本:

JS JS

var startX = 0,
    startY = 0;

$('.imgLink').click(function(){
    draw($('href'));
});

function draw(image){
    image = image.get(0);
    var ctx = document.getElementById('myCanvas').getContext('2d');
    ctx.drawImage(image,startX,startY,20,20);
    startY+=20;
}

HTML 的HTML

        <canvas id="myCanvas"></canvas>
<hr />
    <a href="http://www.avatarsdb.com/avatars/blue_eyed_tiger.jpg" class="imgLink">1</a>
    <a href="www.tiger-explorer.com/avatars/Tiger%20Theme/tiger006.jpg" class="imgLink">2</a>
    <a href="http://www.tiger-explorer.com/avatars_uploaded/avatar_522_1369588658.gif" class="imgLink">3</a>
    <a href="www.avatarsdb.com/avatars/angry_tiger.gif" class="imgLink">4</a>
    <a href="http://www.teesnthings.com/ProductImages/animal/wildlife-t-shirts/tiger-t-shirts/striking-tiger-t-shirt.jpg" class="imgLink">5</a>

Here is JSFIDDLE 这是JSFIDDLE

Three problems: 三个问题:

  1. Make sure all of your links properly have 'http://' in their href 确保所有链接的href正确包含“ http://”
  2. Make sure that the selector is appropriate 确保选择器合适
  3. You have to load the image into a javascript object in order to dynamically use it 您必须将图片加载到javascript对象中才能动态使用它
  4. You have to prevent the default method of the link, otherwise, it just opens the link to the image 您必须阻止链接的默认方法,否则,它只会打开指向图像的链接

To correct #2, change your line: 要更正#2,请更改行:

draw($('href'));

into: 变成:

draw($(this).attr('href'));

This will properly select the href property you wish to draw to the canvas based on the specific link that was clicked 这将根据所单击的特定链接正确选择要绘制到画布上的href属性。

To correct #3, you can add the following changes: 要更正#3,您可以添加以下更改:

    var startX = 0,
        startY = 0;

    $('.imgLink').click(function(e){
        e.preventDefault();
        draw($(this).attr('href'));
    });

    function draw(image){
        var newImg = new Image;
        newImg.onload = function() {
        var ctx = document.getElementById('myCanvas').getContext('2d');
        ctx.drawImage(newImg,startX,startY,20,20);
        startY+=20;
          }
        newImg.src = image;


    }

what you are missing is, you need to create an image element and draw it in the canvas, it can be done like: 您所缺少的是,您需要创建一个图像元素并将其绘制在画布中,可以像这样完成:

var startX = 0,
    startY = 0;

$('.imgLink').click(function(e){
    e.preventDefault();
   draw(e.target.href);
});

function draw(src){        
    var ctx = document.getElementById('myCanvas').getContext('2d');
    var img = document.createElement('img');
    img.onload = function(){
    ctx.drawImage(img,startX,startY,20,20);
    startY+=20;
    }
    img.src = src;
}

Fiddle Demo 小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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