canvas 绘制html元素,javascript – 使用jquery和canvas从html元素id到另一个html元素绘制线条...

我做了一个非常粗略的例子,说明我是怎么做的.它应该让你在正确的轨道上:

我将class line用于hoverable元素,并使用data-id属性作为相应的元素.

HTML:

This is my text with this very important word

...

important.jpg


o asf isj biojso jo f ad f

important.jpg

jQuery的:

$('.line').hover(function () {

var $t = $(this);

var $i = $('#' + $t.data('id'));

// find offset positions for the word (t = this) and image (i)

var ot = {

x: $t.offset().left + $t.width() / 2,

y: $t.offset().top + $t.height() / 2

};

var oi = {

x: $i.offset().left + $i.width() / 2,

y: $i.offset().top + $i.height() / 2

};

// x,y = top left corner

// x1,y1 = bottom right corner

var p = {

x: ot.x < oi.x ? ot.x : oi.x,

x1: ot.x > oi.x ? ot.x : oi.x,

y: ot.y < oi.y ? ot.y : oi.y,

y1: ot.y > oi.y ? ot.y : oi.y

};

// create canvas between those points

var c = $('').attr({

'width': p.x1 - p.x,

'height': p.y1 - p.y

}).css({

'position': 'absolute',

'left': p.x,

'top': p.y,

'z-index': 1

}).appendTo($('body'))[0].getContext('2d');

// draw line

c.strokeStyle = '#f00';

c.lineWidth = 2;

c.beginPath();

c.moveTo(ot.x - p.x, ot.y - p.y);

c.lineTo(oi.x - p.x, oi.y - p.y);

c.stroke();

}, function () {

$('canvas').remove();

});

演示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值