2017-09-27 67 views
0

我試圖在託管平臺上實現一項功能,該功能確實不允許使用PHP的。只有JS & HTML。而我堅持!使用node.js(或其他替代品)在服務器上保存畫布

我已經建立了這個畫布,讓你的畫,然後通過document.getElementById("canvasimg")

我想要的圖像以某種方式被保存到服務器並在新用戶的訪問重新叫保存,所以他們會看到什麼已被最後保存在畫布上。

我正在考慮在用戶保存畫布的新版本時覆蓋寫入的託管文件。那麼該圖像可以作爲背景圖像被調用到畫布中,以允許下一個人繼續對其進行繪製?

這裏是我的代碼:

<html> 
 
    <script type="text/javascript"> 
 
    var canvas, ctx, flag = false, 
 
     prevX = 0, 
 
     currX = 0, 
 
     prevY = 0, 
 
     currY = 0, 
 
     dot_flag = false; 
 

 
    var x = "black", 
 
     y = 2; 
 

 
    function init() { 
 
     canvas = document.getElementById('can'); 
 
     ctx = canvas.getContext("2d"); 
 
     w = canvas.width; 
 
     h = canvas.height; 
 

 
     canvas.addEventListener("mousemove", function (e) { 
 
      findxy('move', e) 
 
     }, false); 
 
     canvas.addEventListener("mousedown", function (e) { 
 
      findxy('down', e) 
 
     }, false); 
 
     canvas.addEventListener("mouseup", function (e) { 
 
      findxy('up', e) 
 
     }, false); 
 
     canvas.addEventListener("mouseout", function (e) { 
 
      findxy('out', e) 
 
     }, false); 
 
    } 
 

 
    function color(obj) { 
 
     switch (obj.id) { 
 
      case "black": 
 
       x = "black"; 
 
       break; 
 
      case "white": 
 
       x = "white"; 
 
       break; 
 
     } 
 
     if (x == "white") y = 14; 
 
     else y = 2; 
 

 
    } 
 

 
    function draw() { 
 
     ctx.beginPath(); 
 
     ctx.moveTo(prevX, prevY); 
 
     ctx.lineTo(currX, currY); 
 
     ctx.strokeStyle = x; 
 
     ctx.lineWidth = y; 
 
     ctx.stroke(); 
 
     ctx.closePath(); 
 
    } 
 

 
    function erase() { 
 
     var m = confirm("Want to clear"); 
 
     if (m) { 
 
      ctx.clearRect(0, 0, w, h); 
 
      document.getElementById("canvasimg").style.display = "none"; 
 
     } 
 
    } 
 

 
    function save() { 
 
     document.getElementById("canvasimg").style.border = "2px solid"; 
 
     var dataURL = canvas.toDataURL(); 
 
     document.getElementById("canvasimg").src = dataURL; 
 
     document.getElementById("canvasimg").style.display = "inline"; 
 
    } 
 

 
    function findxy(res, e) { 
 
     if (res == 'down') { 
 
      prevX = currX; 
 
      prevY = currY; 
 
      currX = e.clientX - canvas.offsetLeft; 
 
      currY = e.clientY - canvas.offsetTop; 
 

 
      flag = true; 
 
      dot_flag = true; 
 
      if (dot_flag) { 
 
       ctx.beginPath(); 
 
       ctx.fillStyle = x; 
 
       ctx.fillRect(currX, currY, 2, 2); 
 
       ctx.closePath(); 
 
       dot_flag = false; 
 
      } 
 
     } 
 
     if (res == 'up' || res == "out") { 
 
      flag = false; 
 
     } 
 
     if (res == 'move') { 
 
      if (flag) { 
 
       prevX = currX; 
 
       prevY = currY; 
 
       currX = e.clientX - canvas.offsetLeft; 
 
       currY = e.clientY - canvas.offsetTop; 
 
       draw(); 
 
      } 
 
     } 
 
    } 
 
    </script> 
 
    <body onload="init()"> 
 
     <canvas id="can" width="500px" height="675px" style="position:absolute;border:2px solid;background:url(http://files.cargocollective.com/715286/sticker.jpg);background-size:100%100%;"></canvas> 
 
     <div style="position:relative;top:40px;left:600px;">Eraser</div> 
 
     <div style="position:relative;top:50px;left:600px;width:15px;height:15px;background:white;border:2px solid;" id="white" onclick="color(this)"></div> 
 
     <div style="position:relative;top:100px;left:600px;width:15px;height:15px;background:black;border:2px solid;" id="black" onclick="color(this)"></div> 
 
     <div style="position:relative;top:50px;left:600px;">Pen</div> 
 
     <img id="canvasimg" style="position:relative;top:10%;left:600px;" style="display:none;"> 
 
     <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:relative;top:150px;left:594px;"> 
 
     <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:relative;top:180px;left:550px;"> 
 
    </body> 
 
    </html>

回答

0

你不能。

如果您不能修改服務器(例如PHP),那麼您肯定無法使用NodeJS。

您可以做的唯一的事情就是將畫布保存到localStoragesessionStorage但這些只是暫時的。

相關問題