简体   繁体   English

将HTML5画布详细信息保存到MySql

[英]Save HTML5 canvas Details to MySql

i'm new to HTML5 and i just want to know how can i save the canvas data ( i have drawn a square on the canvas ). 我是HTML5的新手,我只想知道如何保存画布数据(我在画布上绘制了一个正方形)。 I wanna save x,y,h,w or scale on the database and use that data to recreate the drawing again. 我想将x,y,h,w或比例尺保存在数据库中,然后使用该数据再次重新创建图形。 How can i do this ? 我怎样才能做到这一点 ?

Thank You. 谢谢。

Well you could store them into a mysql database or a server file. 好吧,您可以将它们存储到mysql数据库或服务器文件中。

In case of mysql create a table named entities with the columns you want or even 1 column and serialize the data you want to store. 如果是mysql,请使用所需的列或什至1列创建一个名为实体的表,并序列化要存储的数据。

For example 例如

id | x | y | h | w | scale
1    4   4   4   4    0.5

or 要么

$object_cube = array();
$object_bube['x'] = 4;
$object_bube['y'] = 4;
$object_bube['h'] = 4;
$object_bube['w'] = 4;
$object_bube['s'] = 0.5;

then serialize and create your PDO query 然后序列化并创建您的PDO查询

$host =     "localhost";
$user =     "someuser";
$pass =     "somepass";
$database = "db_name";

$db = new PDO("mysql:host=$host;dbname=$database", $user, $pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) or die("Connection error!");
$ser_obj = serialize($object_cube);
$query = "INSERT INTO entities ('cube') VALUES (:object)";
$params = array(':object' => $ser_obj );
db_query($query, $params);

function db_query($q,$p = array())
{
    global $db;

    if($stmt = $db->prepare($query))
    {

        if(count($p) > 0)
        {
            foreach ($p as $key => $value) {
                $stmt->bindParam($key,$value);
            }
        }

        $stmt->execute();

    }

}

this should take care of saving after that is up to you to load what you want whenever you need it by mining the database :) 这应该节省下来,这取决于您是否需要通过挖掘数据库来加载所需的内容:)

You 'll have to be familiar with AJAX -> http://www.w3schools.com/ajax/default.asp and some server side language like php or asp. 您将必须熟悉AJAX-> http://www.w3schools.com/ajax/default.asp和一些服务器端语言,例如php或asp。 Otherwise you could store it locally. 否则,您可以将其存储在本地。

In order to access a database on a server, you will need a server-sided application which receives the data from the users web browser and then sends it to the database. 为了访问服务器上的数据库,您将需要一个服务器端应用程序,该应用程序从用户Web浏览器接收数据,然后将其发送到数据库。

The communication between client an server could be implemented with XmlHttpRequest or WebSockets . 客户端与服务器之间的通信可以使用XmlHttpRequestWebSockets实现

The web application which runs on the server could be implemented in NodeJS, PHP, ASP.NET, JSP, Django, Ruby on Rails or any other server-sided web development technology. 在服务器上运行的Web应用程序可以用NodeJS,PHP,ASP.NET,JSP,Django,Ruby on Rails或任何其他服务器端Web开发技术来实现。

Alternatively, when you want to do it without server-sided programming, you could also store the data locally in the users localStorage . 另外,如果您希望在不进行服务器端编程的情况下执行此操作,则还可以将数据本地存储在用户localStorage中

A possible solution is to store all the object data in an array and serialize it before sending it to the server: 一种可能的解决方案是将所有对象数据存储在一个数组中并进行序列化,然后再将其发送到服务器:

GraphicObjects = new Array();
GraphicObjects.push({
    x: 10,
    y: 10,
    w: 100,
    h: 200,
    scale: 0.5
}); // Write a function for adding new objects, that's just an example
$.post('urlToPostTo', data: GraphicObjects.stringify());

Later retrieve the JSON data back to redraw your image. 稍后取回JSON数据以重绘图像。

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

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