我正在动态地将多个图像添加到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. 将元素添加到画布JavaFX
- 6. 将元素添加到XAML画布C#
- 7. 将边框添加到画布元素
- 8. 添加自动超链接到图像
- 9. 如何将画布中的图像添加到expandablelistview(组)
- 10. 将元素添加到链接列表?
- 11. 在ReportLab中将超链接添加到画布元素的最简单方法是什么?
- 12. 试图将超链接添加到图像
- 13. 如何将超链接添加到现有PDF文件中的图像?
- 14. 如何将多条多段线添加到画布元素?
- 15. 如何将链接元素添加到json对象中
- 16. 如何添加从画布元素创建的图像到页面?
- 17. 将图像添加到画布不允许将画布保存为图像
- 18. OpenCart:如何添加链接到图像
- 19. 如何将图像添加到css3中的伪元素之前::
- 20. 添加图标到(链接)元/元素元素
- 21. 如何将元素添加到已排序的链接列表?
- 22. jQuery Mobile的 - 如何将链接添加到列表元素
- 23. JQuery Tools Scrollable:不可能将超链接添加到图像
- 24. 将超链接添加到javascript图像阵列
- 25. 将图像悬停添加到链接
- 26. 将图像添加到数组元素
- 27. 将图像添加到Array.push元素
- 28. C#Excel如何在单元格链接中添加超链接
- 29. 如何将超链接添加到背景图片?
- 30. 如何使用fabricjs将多个图像添加到画布
请包括您尝试使用的代码。 – K3N