日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html中放大镜案列,Canvas实现放大镜效果完整案例分析(附代码)

發布時間:2025/4/5 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html中放大镜案列,Canvas实现放大镜效果完整案例分析(附代码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文主要記錄 canvas 在圖像、文字處理、離屏技術和放大鏡特效的實現過程中使用到的api。先看下效果吧:

一張模糊的圖片:

鼠標點擊任意位置,產生放大效果:

哇塞~ 一個帥哥,哈哈哈哈~

1、放大鏡原理

實現效果:如上圖,點擊或點擊滑動鼠標顯示一個區域,區域中顯示對應點擊部分范圍的放大清晰圖片。那么問題就可以肢解為3部分:

1、如何在canvas(模糊圖)上再畫出另外一個canvas(清晰放大圖);

2、如何將canvas中顯示的(清晰放大圖)剪切出圓形區域。

3、如何在鼠標點擊滑動的時候顯示該區域;

2、顯示模糊照片

其實一般的交互不是模糊照片,這里我只是為了夸張下效果,用了張模糊的原圖,哈哈哈,canvas本身是可以對清晰的圖片做濾鏡處理,涉及到很多圖形學的算法,然后我不會,默默的打開了ps手動高斯模糊了一張照片...嗯,沒毛病!

首先定義一個 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、加載清晰圖片

我們再加一個canvas(放大鏡看到的圖片),初始隱藏:

var canvas2 = document.getelementbyid('canvas2'),

ctx2 = canvas2.getcontext('2d'),

scale = 2; // 放大倍數

// 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 繪制放大鏡

鼠標所處的位置點(x,y)是區域的中心,計算出清晰圖所在的位置點,截取圓形

// 保存當前狀態

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);

// 釋放當前狀態

ctx1.restore();

繪制狀態的最佳搭檔save()和restore():用于對當前狀態的保存及釋放不影響其他操作;可用于重復的繪制圖形的變化過程;

clip():表示剪切區域

4.2 離屏技術

所謂的離屏技術就是在一個canvas上繪制另外一個canvas,前面使用的繪制圖片的api是 drawimage() ,它分別可以支持,3個、5個和9個參數; 其中第一個參數既可以是圖片,也可以是canvas對象!那么我們就可以使用這個方法,在圓上繪制出清晰圖了~

// 圓的半徑是mr

var mr = 100;

// 對應模糊圖的左上角起點

var dx = x - mr,

dy = y - mr;

// 找出清晰圖截圖的左上角起點

var sx = x * scale - mr,

sy = y * scale- mr;

...

ctx.clip();

// 在對應的位置上重新繪制圖片

//drawimage(img,sx,sy,swidth,sheight,x,y,width,height) 9個參數時

//img: 圖片/canvas

//sx: 圖片的x起點

//sy: 圖片的y起點

//swidth:要繪制的圖片選取的寬度

//sheight:要繪制的圖片選取的高度

//x,y:圖片在canvas上顯示的位置

//width,height:在canvas上要顯示的大小

ctx1.drawimage(canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);

...

5、鼠標交互事件

效果:鼠標點擊并滑動鼠標產生反應,松開鼠標或者移除畫布則失效。

//定義一個判斷鼠標是否是點擊滑動的標識符

var flag;

//鼠標點入事件

canvas.onmousedown = function(e) {

flag = true;

//顯示放大鏡

}

// 鼠標移動事件

canvas.onmousemove = function(e) {

if (flag) {

//顯示放大鏡

}

}

//鼠標松開事件

canvas.onmouseup = function(e) {

flag = false;

// 隱藏放大鏡

}

//鼠標離開事件

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();

},

//移動

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();

}

到此這篇關于canvas實現放大鏡效果完整案例分析(附代碼)的文章就介紹到這了,更多相關canvas 放大鏡內容請搜索萬仟網以前的文章或繼續瀏覽下面的相關文章,希望大家以后多多支持萬仟網!

總結

以上是生活随笔為你收集整理的html中放大镜案列,Canvas实现放大镜效果完整案例分析(附代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。