首頁 > web前端 > js教程 > 主體

JavaScript html5 canvas實作圖片上畫超鏈接

小云云
發布: 2017-12-09 14:42:23
原創
1925 人瀏覽過

本文主要為大家詳細介紹了JavaScript html5 canvas實現圖片上畫超鏈接,具有一定的參考價值,有興趣的小伙伴們可以參考一下,希望能幫助到大家。

本文範例為大家分享了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(&#39;canvasFile&#39;), 
      ctx = canvas.getContext(&#39;2d&#39;), 
      img = new Image; 
    var btnDone = document.getElementById(&#39;btnDone&#39;); 
    var btnRedo = document.getElementById(&#39;btnRedo&#39;); 
 
 
    ctx.strokeStyle = &#39;#FF0000&#39;; 
 
    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 = &#39;red&#39;; 
      ctx.fill(); 
      ctx.lineWidth = 2; 
      ctx.strokeStyle = &#39;#FF0000&#39;; 
      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(&#39;touchmove&#39;, touchmove, false); 
      canvas.addEventListener(&#39;touchend&#39;, 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(&#39;canvasFile&#39;).toDataURL("image/jpeg"); 
      image = image.replace(&#39;data:image/jpeg;base64,&#39;, &#39;&#39;); 
      $("#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(); 
    }
登入後複製

#相關推薦:

##js實作頁面加載時自動觸發超連結實例詳解

html中超連結a實例用法匯總

html和css中設定超連結樣式方法的總結

以上是JavaScript html5 canvas實作圖片上畫超鏈接的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!