• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

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

    发布者: 福建二哥 | 发布时间: 2025-6-16 12:27| 查看数: 76| 评论数: 0|帖子模式

    本文主要记录
    1. canvas
    复制代码
    在图像、文字处理、离屏技术和放大镜特效的实现过程中使用到的API。先看下效果吧:
    一张模糊的图片:

    鼠标点击任意位置,产生放大效果:

    哇塞~ 一个帅哥,哈哈哈哈~
    1、放大镜原理
    实现效果:如上图,点击或点击滑动鼠标显示一个区域,区域中显示对应点击部分范围的放大清晰图片。那么问题就可以肢解为3部分:
    1、如何在canvas(模糊图)上再画出另外一个canvas(清晰放大图);
    2、如何将canvas中显示的(清晰放大图)剪切出圆形区域。
    3、如何在鼠标点击滑动的时候显示该区域;

    2、显示模糊照片
    其实一般的交互不是模糊照片,这里我只是为了夸张下效果,用了张模糊的原图,哈哈哈,canvas本身是可以对清晰的图片做滤镜处理,涉及到很多图形学的算法,然后我不会,默默的打开了PS手动高斯模糊了一张照片...嗯,没毛病!
    首先定义一个
    1. canvas
    复制代码
    元素
    1.   <canvas id="canvas"></canvas>
    复制代码
    1. //定义canvas画布
    2. var canvas1 = document.getElementById('canvas1');
    3.     ctx1 = canvas.getContext('2d');

    4. //模糊图片加载
    5. var image1 = new Image();
    6. image1.src = "./模糊.png";

    7. //图片加载成功后绘制图片
    8. image1.onload = function() {
    9.   //drawImage 在画布上绘制模糊图片
    10.   ctx1.drawImage(image1, 0, 0, canvas1.width, canvas1.height);
    11. };     
    复制代码
    ctx1.drawImage(图片,x位置,y位置,图片显示宽度,图片显示高度)
    3、加载清晰图片
    我们再加一个canvas(放大镜看到的图片),初始隐藏:
    1. <canvas id="canvas2" style="display:none"></canvas>
    复制代码
    1. var canvas2 = document.getElementById('canvas2'),
    2.     ctx2 = canvas2.getContext('2d'),
    3.     scale = 2; // 放大倍数

    4. // canvas2的图片大小是模糊图片的2倍
    5. canvas2.width = canvas.width * scale;
    6. canvas2.height = canvas.height * scale;

    7. // 在canvas2中加载图片
    8. var image2 = new Image();
    9. image2.src = "name2.png";

    10. image2.onload = function() {
    11.   ctx2.drawImage(image2, 0, 0, canvas2.width, canvas2.height);
    12. };
    复制代码
    4、显示放大镜
    4.1 绘制放大镜
    鼠标所处的位置点(x,y)是区域的中心,计算出清晰图所在的位置点,截取圆形
    1. // 保存当前状态
    2. ctx1.save();
    3. // 边框
    4. ctx1.strokeStyle = "#9eddf1";
    5. ctx1.lineWidth = 3;
    6. // 开始绘制
    7. ctx1.beginPath();
    8. // ⚪
    9. ctx1.arc(x, y, mr, 0, Math.PI * 2);
    10. ctx1.stroke();
    11. // 表示剪切
    12. ctx1.clip();

    13. // 画图
    14. ctx1.drawImage(that.canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);

    15. // 释放当前状态
    16. ctx1.restore();
    复制代码

    绘制状态的最佳搭档save()和restore():用于对当前状态的保存及释放不影响其他操作;可用于重复的绘制图形的变化过程;
    clip():表示剪切区域
    4.2 离屏技术
    所谓的离屏技术就是在一个canvas上绘制另外一个canvas,前面使用的绘制图片的API是
    1. drawImage()
    复制代码
    ,它分别可以支持,3个、5个和9个参数; 其中第一个参数既可以是图片,也可以是canvas对象!那么我们就可以使用这个方法,在圆上绘制出清晰图了~
    1. // 圆的半径是mr
    2. var mr = 100;

    3. // 对应模糊图的左上角起点
    4. var dx = x - mr,
    5.     dy = y - mr;

    6. // 找出清晰图截图的左上角起点
    7. var sx = x * scale - mr,
    8.     sy = y * scale- mr;
    复制代码
    1.    ...
    2.     ctx.clip();
    3.     // 在对应的位置上重新绘制图片
    4.     //drawImage(img,sx,sy,swidth,sheight,x,y,width,height) 9个参数时
    5.     //img: 图片/canvas
    6.     //sx: 图片的X起点
    7.     //sy: 图片的Y起点
    8.     //swidth:要绘制的图片选取的宽度
    9.     //sheight:要绘制的图片选取的高度
    10.     //x,y:图片在canvas上显示的位置
    11.     //width,height:在Canvas上要显示的大小
    12.     ctx1.drawImage(canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);   
    13.     ...
    复制代码
    5、鼠标交互事件
    效果:鼠标点击并滑动鼠标产生反应,松开鼠标或者移除画布则失效。
    1. //定义一个判断鼠标是否是点击滑动的标识符
    2. var flag;

    3. //鼠标点入事件
    4. canvas.onmousedown = function(e) {
    5.   flag = true;
    6.   //显示放大镜
    7. }
    8. // 鼠标移动事件
    9. canvas.onmousemove = function(e) {
    10.   if (flag) {
    11.     //显示放大镜
    12.   }
    13. }
    14. //鼠标松开事件
    15. canvas.onmouseup = function(e) {
    16.   flag = false;
    17.   // 隐藏放大镜
    18. }
    19. //鼠标离开事件
    20. canvas.onmouseout = function(e) {
    21.   flag = false;
    22.   // 隐藏放大镜
    23. }
    复制代码
    完整代码:
    1. var scale = 3;
    2. var mr = 150;
    3. var photo = {
    4.   //初始化
    5.   init: function() {
    6.     var that = this;

    7.     that.canvas = document.getElementById('canvas');
    8.     that.ctx = that.canvas.getContext('2d');

    9.     that.canvas2 = document.getElementById('canvas2');
    10.     that.ctx2 = that.canvas2.getContext('2d');

    11.     that.canvas.width = 800;
    12.     that.canvas.height = 500;

    13.     that.canvas2.width = that.canvas.width * scale;
    14.     that.canvas2.height = that.canvas.height * scale;
    15.     that.image1 = new Image();
    16.     that.image1.src = "./name3.jpg";

    17.     that.image2 = new Image();
    18.     that.image2.src = "./name4.jpg";

    19.     that.image1.onload = function() {
    20.       that.ctx.drawImage(that.image1, 0, 0, that.canvas.width, that.canvas.height);
    21.     };

    22.     that.image2.onload = function() {
    23.       that.ctx2.drawImage(that.image2, 0, 0, that.canvas2.width, that.canvas2.height);
    24.       that.moveEvt();
    25.     };
    26.   },

    27.   bigerImage: function(x, y) {
    28.     var that = this;
    29.     var imageX = x * scale,
    30.       imageY = y * scale,
    31.       sx = imageX - mr,
    32.       sy = imageY - mr;

    33.     var dx = x - mr,
    34.       dy = y - mr;

    35.     that.ctx.save();

    36.     that.ctx.strokeStyle = "#9eddf1";
    37.     that.ctx.lineWidth = 3;

    38.     that.ctx.beginPath();
    39.     that.ctx.arc(x, y, mr, 0, Math.PI * 2);


    40.     that.ctx.shadowColor = "#6ed25b";
    41.     that.ctx.shadowBlur = 10;

    42.     that.ctx.stroke();
    43.     that.ctx.clip();

    44.     that.ctx.drawImage(that.canvas2, sx, sy, 2 * mr, 2 * mr, dx, dy, 2 * mr, 2 * mr);

    45.     that.ctx.restore();
    46.   },

    47.   //移动
    48.   moveEvt: function() {
    49.     var that = this;
    50.     that.canvas.onmousedown = function(e) {
    51.             that.flag = true;
    52.       that.showImage(e);
    53.     }

    54.     that.canvas.onmousemove = function(e) {
    55.       if (that.flag) {
    56.         that.showImage(e)
    57.       }
    58.     }

    59.     that.canvas.onmouseup = function(e) {
    60.       that.hideImage(e)
    61.     }

    62.     that.canvas.onmouseout = function(e) {
    63.       that.hideImage(e)
    64.     }
    65.   },

    66.   showImage: function(e) {
    67.     e.preventDefault()
    68.     var x = e.offsetX,
    69.       y = e.offsetY,
    70.       that = this;
    71.     that.ctx.clearRect(0, 0, that.canvas.width, that.canvas.height);
    72.     that.ctx.drawImage(that.image1, 0, 0, that.canvas.width, that.canvas.height);
    73.     that.bigerImage(x, y);
    74.   },

    75.   hideImage: function(e) {
    76.     e.preventDefault()
    77.     var that = this;
    78.    
    79.     that.flag = false;
    80.     that.ctx.clearRect(0, 0, that.canvas.width, that.canvas.height);
    81.     that.ctx.drawImage(that.image1, 0, 0, that.canvas.width, that.canvas.height);
    82.   }
    83. }

    84. window.onload = function() {
    85.   photo.init();
    86. }
    复制代码
    到此这篇关于Canvas实现放大镜效果完整案例分析(附代码)的文章就介绍到这了,更多相关Canvas 放大镜内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/753846.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表