JavaScript html5 canvas实现图片上画超链接

作者:_iorilan 时间:2024-04-28 10:19:19 

本文实例为大家分享了html5 canvas在图片上画超链接的具体代码,供大家参考,具体内容如下

1. html


<canvas id="canvasFile" style="margin-top:15px;" width="500" height="400"></canvas>
<input type="button" id="btnRedo" value="Re-Draw" class="btn btn-warning"/>

2. javascript


var photoW = 400;
   var photoH = 300;
   var photo;

// logic load image into canvas
   // ...
   // e.g.  
   // photo = new Image();
   // photo.onload = function() {
   // draw photo into canvas when ready
   // ctx.drawImage(photo, 0, 0, photoW, photoH);
   // };
   // load photo into canvas
   // photo.src = picURL;

// canvas highlight
   var canvas = document.getElementById('canvasFile'),
     ctx = canvas.getContext('2d'),
     img = new Image;
   var btnDone = document.getElementById('btnDone');
   var btnRedo = document.getElementById('btnRedo');

ctx.strokeStyle = '#FF0000';

function DrawDot(x, y) {
     var centerX = x;
     var centerY = y;
     var radius = 2;

ctx.beginPath();
     ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
     ctx.fillStyle = 'red';
     ctx.fill();
     ctx.lineWidth = 2;
     ctx.strokeStyle = '#FF0000';
     ctx.stroke();
   }

function startDrawing() {
     ctx.drawImage(img, 0, 0, photoW, photoH);
     canvas.onmousemove = mousemoving;
     canvas.onmousedown = mousedownhandle;
     canvas.onmouseup = mouseuphandle;
     // ## mobile events
     //touchstart – to toggle drawing mode “on”
     //touchend – to toggle drawing mode “off”
     //touchmove – to track finger position, used in drawing
     canvas.addEventListener('touchmove', touchmove, false);
     canvas.addEventListener('touchend', mouseuphandle, false);

btnRedo.onclick = function (e) {
       ctx.clearRect(0, 0, ctx.width, ctx.height);
       ctx.drawImage(photo, 0, 0, photoW, photoH);
       savedrawing();
     }
   }
   function savedrawing(e) {
     var image = document.getElementById('canvasFile').toDataURL("image/jpeg");
     image = image.replace('data:image/jpeg;base64,', '');
     $("#imgNric1").val(image);
   };

function mousemoving(e) {
     if (drawing) {
       mousedownhandle(e);
     }
   }

var drawing = false;

function mousedownhandle(e) {
     drawing = true;
     var r = canvas.getBoundingClientRect(),
       x = e.clientX - r.left,
       y = e.clientY - r.top;

DrawDot(x, y);
   }  

function mouseuphandle(e) {
     savedrawing();
     e.preventDefault();
     drawing = false;

}

//// mobile touch events
   function touchmove(e) {
     if (e.clientX > 800) {
       mousedownhandle(e);
       return;
     }

var r = canvas.getBoundingClientRect(),
       //event.changedTouches[0].pageX + ":" + event.changedTouches[0].pageY;
       x = e.changedTouches[0].pageX - r.left,
       y = e.changedTouches[0].pageY - r.top;

DrawDot(x, y);
     e.preventDefault();
   }

来源:http://blog.csdn.net/lan_liang/article/details/78276117

标签:JavaScript,html5,canvas,超链接
0
投稿

猜你喜欢

  • Go语言实现彩色输出示例详解

    2023-09-14 01:19:45
  • JavaScript学习笔记之函数记忆

    2024-04-16 09:27:01
  • 判断字段是否被更新 新旧数据写入Audit Log表中

    2012-01-29 17:56:33
  • js 数据存储和DOM编程

    2024-02-24 07:52:31
  • pytorch多GPU并行运算的实现

    2022-01-09 22:01:06
  • Python 12306抢火车票脚本 Python京东抢手机脚本

    2023-02-13 14:22:21
  • 献给写作者的 Markdown 新手指南

    2023-04-14 04:50:18
  • python循环定时中断执行某一段程序的实例

    2023-10-09 22:11:20
  • 支持多类型数据库的c#数据库模型示例

    2024-01-13 16:41:15
  • Python高级特性——详解多维数组切片(Slice)

    2023-01-20 17:04:13
  • PHP simplexml_load_string()函数实例讲解

    2023-07-09 07:19:28
  • JS设置cookie、读取cookie、删除cookie

    2023-08-27 19:35:17
  • 使用wxpy实现自动发送微信消息功能

    2022-04-01 21:16:06
  • Python super( )函数用法总结

    2022-07-09 23:23:10
  • Django 状态保持搭配与存储的实现

    2021-05-10 04:16:00
  • python3中宏HAVE_VFORK的使用

    2021-05-22 07:22:21
  • mysql 批处理文件出错后继续执行的实现方法

    2024-01-19 13:25:09
  • python自动化发送邮件实例讲解

    2023-11-11 16:01:41
  • response.setHeader()方法设置http文件头的值

    2010-03-11 22:43:00
  • 判断浏览器是否接受 Cookie

    2009-07-28 17:52:00
  • asp之家 网络编程 m.aspxhome.com