2013-08-02 2 views

답변

1

나는 그것을 작동시켰다. 여기에 여기에 [DEMO]

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(); 
+0

데모가 없습니다. 깨진 링크. 가능한 경우 수정하십시오. –

+0

작동하지 마십시오! – Trunk

+0

위대한 작품! 이 솔루션을 제공해 주셔서 감사합니다. –

2

개별 캔버스 이미지에 하나의 하이퍼 링크를 추가 할 수 없기 때문에 개별 캔버스 이미지에 하이퍼 링크를 추가 할 수 없습니다.

해결 방법은 JavaScript에서 클릭 이벤트를 감지하고 커서가있는 위치와 이미지 위에 있는지 여부를 확인한 다음 그에 따라 페이지를 변경하는 것입니다.

+0

그래서 우리는 이미지의 좌표를 찾아야하고 좌표 위치에 따라 하이퍼 링크를 설정해야합니다. – deepakb

+0

나는 그것을 작동 시켰습니다 ... – deepakb

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에 오신 것을 환영하며 답변을 게시 해 주셔서 감사합니다. 응답 코드에 상황을 추가하는 것을 고려하십시오. –

관련 문제