html中创建画布,html5 – 在HTML画布中创建链接

没有简单的方法。您将不得不将链接文本绘制到画布上,然后检查mouseclick。这是一个演示html页面:

var canvas = document.getElementById("myCanvas");

var ctx;

var linkText="http://stackoverflow.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,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;

}

}

Canvas not supported.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值