我正在動態地將多個圖像添加到HTML5 canvas元素。我想超鏈接這些圖像。我嘗試過不同的方式,但它不適合我。有人知道該怎麼做嗎?如何將超鏈接添加到畫布元素中的圖像?
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. 如何將超鏈接添加到KineticJS畫布中的形狀?
- 2. 如何將圖像添加到畫布
- 3. 如何將圖像添加到html中的超鏈接?
- 4. Wordpress循環並將圖像添加到HTML5畫布元素
- 5. 將元素添加到XAML畫布C#
- 6. 將畫布元素添加到DOM
- 7. 將邊框添加到畫布元素
- 8. 將元素添加到畫布JavaFX
- 9. 添加自動超鏈接到圖像
- 10. 將圖像添加到畫布窗口
- 11. 如何將畫布中的圖像添加到expandablelistview(組)
- 12. 將元素添加到鏈接列表?
- 13. 在ReportLab中將超鏈接添加到畫布元素的最簡單方法是什麼?
- 14. 試圖將超鏈接添加到圖像
- 15. 如何將超鏈接添加到現有PDF文件中的圖像?
- 16. 如何將多條多段線添加到畫布元素?
- 17. 如何添加從畫布元素創建的圖像到頁面?
- 18. 如何將鏈接元素添加到json對象中
- 19. 將圖像添加到畫布不允許將畫布保存爲圖像
- 20. OpenCart:如何添加鏈接到圖像
- 21. 如何使用VBA添加超鏈接到圖像?
- 22. 如何將圖像添加到css3中的僞元素之前::
- 23. 添加圖標到(鏈接)元/元素元素
- 24. jQuery Mobile的 - 如何將鏈接添加到列表元素
- 25. 如何將元素添加到已排序的鏈接列表?
- 26. Prestashop將超鏈接或圖像添加到產品功能
- 27. JQuery Tools Scrollable:不可能將超鏈接添加到圖像
- 28. 將超鏈接添加到javascript圖像陣列
- 29. 將圖像懸停添加到鏈接
- 30. 添加多個元素到畫布js
請包括您嘗試使用的代碼。 – K3N