如何在canvas元素中为图像添加超链接?

4
我正在动态地向HTML5画布元素添加多个图像。我想为这些图像创建超链接。我尝试了不同的方法,但都无法正常工作。请问有谁知道如何实现吗?

请包含您尝试过的代码。 - user1693593
3个回答

2

无法在单个画布图像上添加超链接,因为这些图像成为单个画布元素的一部分。

解决方案是在JavaScript中检测单击事件,确定光标位置以及它是否悬停在图像上,然后相应地更改页面。


所以我们需要找出图像的坐标,然后根据坐标位置设置超链接。 - Deepak Biswal

1
我已经做到了。这是[演示]的链接。
var canvas = document.getElementById("myCanvas");
var ctx;
var linkText="http://google.com";
var linkX=5;
var linkY=15;
var linkHeight=10;
var linkWidth;
var inLink = false;

// draw the balls on the canvas
function draw(){
  canvas = document.getElementById("myCanvas");
  // check if supported
  if(canvas.getContext){

    ctx=canvas.getContext("2d");

    //clear canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    //draw the link
    ctx.font='10px sans-serif';
    ctx.fillStyle = "#0000ff";
    ctx.fillText(linkText,linkX,linkY);
    linkWidth=ctx.measureText(linkText).width;

    //add mouse listeners
    canvas.addEventListener("mousemove", on_mousemove, false);
    canvas.addEventListener("click", on_click, false);

  }
}

//check if the mouse is over the link and change cursor style
function on_mousemove (ev) {
  var x, y;

  // Get the mouse position relative to the canvas element.
  if (ev.layerX || ev.layerX == 0) { //for firefox
    x = ev.layerX;
    y = ev.layerY;
  }
  x-=canvas.offsetLeft;
  y-=canvas.offsetTop;

  //is the mouse over the link?
  if(x>=linkX && x <= (linkX + linkWidth) && y<=linkY && y>= (linkY-linkHeight)){
      document.body.style.cursor = "pointer";
      inLink=true;
  }
  else{
      document.body.style.cursor = "";
      inLink=false;
  }
}

//if the link has been clicked, go to link
function on_click(e) {
  if (inLink)  {
    window.location = linkText;
  }
}

draw();

未找到演示。链接失效。如有可能,请进行更正。 - Fernando Leal
工作得很好!感谢提供这个解决方案。 - Chris Nielsen
很想看到工作演示。我采用了一种不同的方法,在图像顶部嵌入了彩色标记。标记表示的链接类型确定了标记头的颜色。通过JQuery将标记拖放到图像上,并实时将标记位置与超链接和类型一起记录在数据库中。加载图像时,我根据它们的类型在图像上方的画布层上放置标记,并记录它们在该数据库中的位置。 - IlludiumPu36

0

var canvas = document.getElementById("myCanvas");
var ctx;
var linkText="http://google.com";
var linkX=5;
var linkY=15;
var linkHeight=10;
var linkWidth;
var inLink = false;

// draw the balls on the canvas
function draw(){
  canvas = document.getElementById("myCanvas");
  // check if supported
  if(canvas.getContext){

    ctx=canvas.getContext("2d");

    //clear canvas
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    //draw the link
    ctx.font='10px sans-serif';
    ctx.fillStyle = "#0000ff";
    ctx.fillText(linkText,linkX,linkY);
    linkWidth=ctx.measureText(linkText).width;

    //add mouse listeners
    canvas.addEventListener("mousemove", on_mousemove, false);
    canvas.addEventListener("click", on_click, false);

  }
}

//check if the mouse is over the link and change cursor style
function on_mousemove (ev) {
  var x, y;

  // Get the mouse position relative to the canvas element.
  if (ev.layerX || ev.layerX == 0) { //for firefox
    x = ev.layerX;
    y = ev.layerY;
  }
  x-=canvas.offsetLeft;
  y-=canvas.offsetTop;

  //is the mouse over the link?
  if(x>=linkX && x <= (linkX + linkWidth) && y<=linkY && y>= (linkY-linkHeight)){
      document.body.style.cursor = "pointer";
      inLink=true;
  }
  else{
      document.body.style.cursor = "";
      inLink=false;
  }
}

//if the link has been clicked, go to link
function on_click(e) {
  if (inLink)  {
    window.location = linkText;
  }
}

draw();
<canvas id="myCanvas" width=500 height=500 style="border:1px solid black"></canvas>

这里是一个可用的链接。


1
欢迎来到SO,并感谢您发布答案。请考虑为您的答案代码添加上下文。 - Richard Erickson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接