HTML5 캔버스 요소에 여러 이미지를 동적으로 추가하고 있습니다. 나는 그 이미지들을 하이퍼 링크하고 싶다. 나는 다른 방법을 시도했지만 그것은 나를 위해 작동하지 않습니다. 아무도 그것을하는 방법을 알고 있습니까?캔버스 요소의 이미지에 하이퍼 링크를 추가하는 방법은 무엇입니까?
3
A
답변
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();
2
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에 오신 것을 환영하며 답변을 게시 해 주셔서 감사합니다. 응답 코드에 상황을 추가하는 것을 고려하십시오. –
관련 문제
- 1. ReportLab의 캔버스 요소에 하이퍼 링크를 추가하는 가장 간단한 방법은 무엇입니까?
- 2. 배경 이미지에 하이퍼 링크를 만드는 방법은 무엇입니까?
- 3. Excel의 탭에 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 4. iPhone 앱에 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 5. JLabel에서 하이퍼 링크를 추가하는 방법
- 6. 하이퍼 링크를 만드는 링크 요소의 목적은 무엇입니까?
- 7. CS5에서 GIF 이미지에 하이퍼 링크를 지정하는 방법은 무엇입니까?
- 8. WMD 편집기 - 이미지에 하이퍼 링크를 만드는 방법은 무엇입니까?
- 9. "jquery image cycle"에서 이미지에 하이퍼 링크를 지정하는 방법은 무엇입니까?
- 10. 데이터베이스에 하이퍼 링크를 추가하는 방법
- 11. gridview에 하이퍼 링크를 추가하는 방법
- 12. 티타늄을 사용하여 이미지에 하이퍼 링크를 지정하는 방법
- 13. Interop를 사용하여 슬라이드에 하이퍼 링크를 추가하는 방법
- 14. InfoPath 양식에서 동적 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 15. EXTJS4- 그리드 열에 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 16. 퓨전 테이블의 텍스트 열에 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 17. 오픈 XML을 사용하여 Word docx에 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 18. apache poi를 사용하여 Excel에 간단한 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 19. jQuery를 사용하여 동적으로 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 20. Javascript에서 div 태그에 동적으로 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 21. Javascript tabify 링크에 추가 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 22. wxpython 앱을 사용하여 하이퍼 링크를 추가하는 방법은 무엇입니까?
- 23. 동적 URL이있는 이미지에 하이퍼 링크하는 방법은 무엇입니까?
- 24. 이미지에 텍스트를 추가하는 방법은 무엇입니까?
- 25. 이미지에 텍스트를 추가하는 방법은 무엇입니까?
- 26. 캔버스 이미지에 json 객체의 이미지를로드하는 방법은 무엇입니까?
- 27. 캔버스 이미지에 방사형 페이드를 적용하는 방법은 무엇입니까?
- 28. JQuery 도구 Scrollable : 이미지에 하이퍼 링크를 추가 할 수 없습니다.
- 29. JQuery를 사용하여 이미지에 HTML을 추가하는 방법은 무엇입니까?
- 30. 하이퍼 링크 요소의 높이를 측정하는 방법은 무엇입니까?
시도한 코드를 포함하십시오. – K3N