简体   繁体   English

从徒手画中保存数据(不保存为图像文件)

[英]Save Data from Freehand Drawing (not as an image file)

I have been trying to save data/object from HTML5 Canvas' drawing feature, how would I save to mySQL table by x and y coordinate points, instead of converting it into a JPEG or an image file?. 我一直在尝试从HTML5 Canvas的绘图功能中保存数据/对象,我如何通过x和y坐标点保存到mySQL表中,而不是将其转换为JPEG或图像文件?

The goal is to save the strokes as data file instead of image file. 目标是将笔划另存为数据文件而不是图像文件。 How can I do this? 我怎样才能做到这一点?

 var canvas = document.getElementById('canvas'), coord = document.getElementById('coord'), ctx = canvas.getContext('2d'), // get 2D context imgCat = new Image(); /*********** draw image *************/ imgCat.src = 'http://c.wearehugh.com/dih5/openclipart.org_media_files_johnny_automatic_1360.png'; imgCat.onload = function() { // wait for image load ctx.drawImage(imgCat, 0, 0); // draw imgCat on (0, 0) }; /*********** handle mouse events on canvas **************/ var mousedown = false; ctx.strokeStyle = '#0000FF'; ctx.lineWidth = 2; canvas.onmousedown = function(e) { var pos = fixPosition(e, canvas); mousedown = true; ctx.beginPath(); ctx.moveTo(pos.x, pos.y); return false; }; canvas.onmousemove = function(e) { var pos = fixPosition(e, canvas); coord.innerHTML = '(' + pos.x + ',' + pos.y + ')'; if (mousedown) { ctx.lineTo(pos.x, pos.y); ctx.stroke(); } }; canvas.onmouseup = function(e) { mousedown = false; }; /********** utils ******************/ // Thanks to http://stackoverflow.com/questions/55677/how-do-i-get-the-coordinates-of-a-mouse-click-on-a-canvas-element/4430498#4430498 function fixPosition(e, gCanvasElement) { var x; var y; if (e.pageX || e.pageY) { x = e.pageX; y = e.pageY; } else { x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; } x -= gCanvasElement.offsetLeft; y -= gCanvasElement.offsetTop; return { x: x, y: y }; } `` 
 <div id="left_col"> <canvas id="canvas" width="900" height="900" style='background-image:url(http://www.robertshadbolt.com/content/01-articles/01-900x900/900x900.gif);' center cetner no-repeat></canvas> <div id="coord" hidden></div> 

Fiddle 小提琴

Canvas cannot handle vector graphics in the sense it will rasterize anything drawn to it. 画布无法处理矢量图形,因为它会光栅化绘制到它的任何内容。 Therefor canvas will only provide you with bitmaps (raw or encoded as an image). 因此,画布只会为您提供位图(原始或编码为图像)。 The canvas can only show the result of those vectors being rasterized, but can never provide them in raw form afterwards. 画布只能显示这些矢量被光栅化的结果,而以后再也不能以原始形式提供它们。

To obtain the effect of dealing with vectors you need to do the following: 为了获得处理向量的效果,您需要执行以下操作:

Track and collect the points in a serializeable format such as an array with literal objects. 以可序列化的格式(例如带有文字对象的数组)跟踪和收集点。 In other words: you need to "record" these points in your mouse handlers parallel to rendering them to canvas. 换句话说:您需要在鼠标处理程序中“记录”这些点,同时将它们渲染到画布上。

You will also need to separate "strokes" (a stroke is from mouse down to mouse up, then create a new array for the next stroke etc. meaning you will need an object with two levels, one to collect stroke arrays, and one array per stroke). 您还需要分开“笔画”(笔画是从鼠标向下移动到鼠标上移,然后为下一个笔画创建一个新数组,依此类推。这意味着您将需要一个具有两个级别的对象,一个对象用于收集笔画数组,一个数组每笔)。

When done you can serialize the array using JSON.stringify() and send to your database. 完成后,您可以使用JSON.stringify()序列化数组并将其发送到数据库。

To restore, read that string back and use JSON.parse() to restore the array. 要还原,请将该字符串读回并使用JSON.parse()还原数组。

Simplified Example 简化示例

 // some dummy point in a serializeable format: var points = [ {x: 10, y: 10}, {x: 20, y: 50}, {x: 100, y: 10} ]; // serialize var str = JSON.stringify(points); document.write("To server: " + str + "<br>"); // send str to database here... // RESTORE // read from database here var restoredPoints = JSON.parse(str); document.write("Point from restored array - x: " + restoredPoints[1].x + " y: " + restoredPoints[1].y); 

In a more real example your object would look like (pseudo code): 在一个更真实的示例中,您的对象看起来像(伪代码):

var strokes = [];

onmousedown: 
   create new array -> strokes.push([]);
   index = strokes.length - 1;
   add point to current stroke -> strokes[index].push({x:x, y:y});

onmousemove:
   add point to current stroke -> strokes[index].push({x:x, y:y});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM