html中放大镜案列,Canvas实现放大镜效果完整案例分析(附代码)
本文主要記錄 canvas 在圖像、文字處理、離屏技術(shù)和放大鏡特效的實(shí)現(xiàn)過程中使用到的api。先看下效果吧:
一張模糊的圖片:
鼠標(biāo)點(diǎn)擊任意位置,產(chǎn)生放大效果:
哇塞~ 一個(gè)帥哥,哈哈哈哈~
1、放大鏡原理
實(shí)現(xiàn)效果:如上圖,點(diǎn)擊或點(diǎn)擊滑動(dòng)鼠標(biāo)顯示一個(gè)區(qū)域,區(qū)域中顯示對(duì)應(yīng)點(diǎn)擊部分范圍的放大清晰圖片。那么問題就可以肢解為3部分:
1、如何在canvas(模糊圖)上再畫出另外一個(gè)canvas(清晰放大圖);
2、如何將canvas中顯示的(清晰放大圖)剪切出圓形區(qū)域。
3、如何在鼠標(biāo)點(diǎn)擊滑動(dòng)的時(shí)候顯示該區(qū)域;
2、顯示模糊照片
其實(shí)一般的交互不是模糊照片,這里我只是為了夸張下效果,用了張模糊的原圖,哈哈哈,canvas本身是可以對(duì)清晰的圖片做濾鏡處理,涉及到很多圖形學(xué)的算法,然后我不會(huì),默默的打開了ps手動(dòng)高斯模糊了一張照片...嗯,沒毛病!
首先定義一個(gè) canvas 元素
//定義canvas畫布
var canvas1 = document.getelementbyid('canvas1');
ctx1 = canvas.getcontext('2d');
//模糊圖片加載
var image1 = new image();
image1.src = "./模糊.png";
//圖片加載成功后繪制圖片
image1.onload = function() {
//drawimage 在畫布上繪制模糊圖片
ctx1.drawimage(image1, 0, 0, canvas1.width, canvas1.height);
};
ctx1.drawimage(圖片,x位置,y位置,圖片顯示寬度,圖片顯示高度)
3、加載清晰圖片
我們?cè)偌右粋€(gè)canvas(放大鏡看到的圖片),初始隱藏:
var canvas2 = document.getelementbyid('canvas2'),
ctx2 = canvas2.getcontext('2d'),
scale = 2; // 放大倍數(shù)
// canvas2的圖片大小是模糊圖片的2倍
canvas2.width = canvas.width * scale;
canvas2.height = canvas.height * scale;
// 在canvas2中加載圖片
var image2 = new image();
image2.src = "name2.png";
image2.onload = function() {
ctx2.drawimage(image2, 0, 0, canvas2.width, canvas2.height);
};
4、顯示放大鏡
4.1 繪制放大鏡
鼠標(biāo)所處的位置點(diǎn)(x,y)是區(qū)域的中心,計(jì)算出清晰圖所在的位置點(diǎn),截取圓形
// 保存當(dāng)前狀態(tài)
ctx1.save();
// 邊框
ctx1.strokestyle = "#9eddf1";
ctx1.linewidth = 3;
// 開始繪制
ctx1.beginpath();
// ?
ctx1.arc(x, y, mr, 0, math.pi * 2);
ctx1.stroke();
// 表示剪切
ctx1.clip();
// 畫圖
ctx1.drawimage(that.canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);
// 釋放當(dāng)前狀態(tài)
ctx1.restore();
繪制狀態(tài)的最佳搭檔save()和restore():用于對(duì)當(dāng)前狀態(tài)的保存及釋放不影響其他操作;可用于重復(fù)的繪制圖形的變化過程;
clip():表示剪切區(qū)域
4.2 離屏技術(shù)
所謂的離屏技術(shù)就是在一個(gè)canvas上繪制另外一個(gè)canvas,前面使用的繪制圖片的api是 drawimage() ,它分別可以支持,3個(gè)、5個(gè)和9個(gè)參數(shù); 其中第一個(gè)參數(shù)既可以是圖片,也可以是canvas對(duì)象!那么我們就可以使用這個(gè)方法,在圓上繪制出清晰圖了~
// 圓的半徑是mr
var mr = 100;
// 對(duì)應(yīng)模糊圖的左上角起點(diǎn)
var dx = x - mr,
dy = y - mr;
// 找出清晰圖截圖的左上角起點(diǎn)
var sx = x * scale - mr,
sy = y * scale- mr;
...
ctx.clip();
// 在對(duì)應(yīng)的位置上重新繪制圖片
//drawimage(img,sx,sy,swidth,sheight,x,y,width,height) 9個(gè)參數(shù)時(shí)
//img: 圖片/canvas
//sx: 圖片的x起點(diǎn)
//sy: 圖片的y起點(diǎn)
//swidth:要繪制的圖片選取的寬度
//sheight:要繪制的圖片選取的高度
//x,y:圖片在canvas上顯示的位置
//width,height:在canvas上要顯示的大小
ctx1.drawimage(canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);
...
5、鼠標(biāo)交互事件
效果:鼠標(biāo)點(diǎn)擊并滑動(dòng)鼠標(biāo)產(chǎn)生反應(yīng),松開鼠標(biāo)或者移除畫布則失效。
//定義一個(gè)判斷鼠標(biāo)是否是點(diǎn)擊滑動(dòng)的標(biāo)識(shí)符
var flag;
//鼠標(biāo)點(diǎn)入事件
canvas.onmousedown = function(e) {
flag = true;
//顯示放大鏡
}
// 鼠標(biāo)移動(dòng)事件
canvas.onmousemove = function(e) {
if (flag) {
//顯示放大鏡
}
}
//鼠標(biāo)松開事件
canvas.onmouseup = function(e) {
flag = false;
// 隱藏放大鏡
}
//鼠標(biāo)離開事件
canvas.onmouseout = function(e) {
flag = false;
// 隱藏放大鏡
}
完整代碼:
var scale = 3;
var mr = 150;
var photo = {
//初始化
init: function() {
var that = this;
that.canvas = document.getelementbyid('canvas');
that.ctx = that.canvas.getcontext('2d');
that.canvas2 = document.getelementbyid('canvas2');
that.ctx2 = that.canvas2.getcontext('2d');
that.canvas.width = 800;
that.canvas.height = 500;
that.canvas2.width = that.canvas.width * scale;
that.canvas2.height = that.canvas.height * scale;
that.image1 = new image();
that.image1.src = "./name3.jpg";
that.image2 = new image();
that.image2.src = "./name4.jpg";
that.image1.onload = function() {
that.ctx.drawimage(that.image1, 0, 0, that.canvas.width, that.canvas.height);
};
that.image2.onload = function() {
that.ctx2.drawimage(that.image2, 0, 0, that.canvas2.width, that.canvas2.height);
that.moveevt();
};
},
bigerimage: function(x, y) {
var that = this;
var imagex = x * scale,
imagey = y * scale,
sx = imagex - mr,
sy = imagey - mr;
var dx = x - mr,
dy = y - mr;
that.ctx.save();
that.ctx.strokestyle = "#9eddf1";
that.ctx.linewidth = 3;
that.ctx.beginpath();
that.ctx.arc(x, y, mr, 0, math.pi * 2);
that.ctx.shadowcolor = "#6ed25b";
that.ctx.shadowblur = 10;
that.ctx.stroke();
that.ctx.clip();
that.ctx.drawimage(that.canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);
that.ctx.restore();
},
//移動(dòng)
moveevt: function() {
var that = this;
that.canvas.onmousedown = function(e) {
that.flag = true;
that.showimage(e);
}
that.canvas.onmousemove = function(e) {
if (that.flag) {
that.showimage(e)
}
}
that.canvas.onmouseup = function(e) {
that.hideimage(e)
}
that.canvas.onmouseout = function(e) {
that.hideimage(e)
}
},
showimage: function(e) {
e.preventdefault()
var x = e.offsetx,
y = e.offsety,
that = this;
that.ctx.clearrect(0, 0, that.canvas.width, that.canvas.height);
that.ctx.drawimage(that.image1, 0, 0, that.canvas.width, that.canvas.height);
that.bigerimage(x, y);
},
hideimage: function(e) {
e.preventdefault()
var that = this;
that.flag = false;
that.ctx.clearrect(0, 0, that.canvas.width, that.canvas.height);
that.ctx.drawimage(that.image1, 0, 0, that.canvas.width, that.canvas.height);
}
}
window.onload = function() {
photo.init();
}
到此這篇關(guān)于canvas實(shí)現(xiàn)放大鏡效果完整案例分析(附代碼)的文章就介紹到這了,更多相關(guān)canvas 放大鏡內(nèi)容請(qǐng)搜索萬仟網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持萬仟網(wǎng)!
總結(jié)
以上是生活随笔為你收集整理的html中放大镜案列,Canvas实现放大镜效果完整案例分析(附代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 长春8中2021年高考 成绩查询,长春八
- 下一篇: 骑士会员卡是什么卡