2017-09-22 107 views
0

我有一個創建了一個畫板的畫布,我想看看它現在可能有保存功能畫布保存到服務器,因此當網站被重新加載上次保存的畫布似乎準備繼續借鑑。保存畫布圖像到主機服務器

這實際上可以讓人們在畫布上進行合作。

我有一個保存功能,現在剛剛呈現在畫布上爲PNG旁邊,原來畫布,但我不能確定如何將它帶到一個新的階段。

在一個單獨的說明 - 這將是一件好事,如果保存按鈕可能會被刪除並保存功能可以每隔幾秒鐘叫 - 但是這可能會非常棘手。

<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

你開到存儲在客戶端上的形象呢?如何在服務器上 - 你有沒有想過如果你想在那裏存儲圖像? –

+0

我想我可以保存在我的服務器上的圖像,然後讓腳本調用保存每3秒越過文件 –

+0

好吧,太棒了!你在運行什麼服務器端框架? 。淨? PHP? –

回答

0

這是你的腳本/ HTML的修改版本,它包裝的IMG成表單標籤和使用setIntervalhttps://www.w3schools.com/jsref/met_win_setinterval.asp調用保存功能,每3秒(在init取消註釋了線啓用)。

你如何保存圖像,一旦它在服務器上的,也許,這將在很大程度上取決於你的情況一個單獨的問題。無論如何保存,圖像數據將通過POST形式傳輸到服務器,您可以在其中隨意使用數據執行任何操作。

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

 
    var saveTimer; 
 
    function onSaveTimer() { 
 
      save();    
 
    } 
 

 

 
    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); 
 
     
 
     // saveTimer = setInterval(onSaveTimer, 3000); 
 
    } 
 

 
    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(); 
 
     if (dataURL) { 
 
      document.getElementById("canvasimg").src = dataURL; 
 
      document.getElementById("canvasimg").style.display = "inline"; 
 
      return true; // will POST form to the server 
 
     } 
 
     return false; // will not POST to server 
 

 
    } 
 

 
    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;"></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> 
 
<form method="POST" action="" onsubmit="save"> 
 
     <img id="canvasimg" style="position:relative;top:10%;left:600px;" style="display:none;"> 
 
</form> 
 
     <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

使用Cookie來保存信息作爲DataURI,然後調用該cookie成圖像元素的src。然後,使用畫布從img元素中拉出圖像。

編輯: 如果您希望將其保存到主機服務器,使用PHP和以下內容:

file_put_contents("/tmp/myFileName.png",base64_decode(str_replace(" ","+",$_GET["data"]))); 

,並保存,只需發送一個請求到PHP頁面變量「數據」設爲canvas.toDataURL()

編輯2: 這裏的非PHP解決方案的示例: http://id0t.x10.mx/StackOverflow/demo1.html

+0

山姆非常感謝你這是一個絕妙的主意! PHP不支持框架我正在使用這麼好的替代方案。你有什麼想法這將是什麼樣子? Im新的URIs –

+0

http://id0t.x10.mx/StackOverflow/demo1.html –

+0

如果您必須使用託管的圖片,請使用類似Imgur的API。 https://apidocs.imgur.com/ –

相關問題