[英]HTML5 saving and restoring state to database
I am trying to build an application using HTML5 that will allow user to add objects like: triangles, circles, squares, etc...and have ability to save these objects on the canvas for the user to return to and continue. 我正在尝试使用HTML5构建一个应用程序,该应用程序将允许用户添加诸如三角形,圆形,正方形等的对象,并能够将这些对象保存在画布上,以供用户返回并继续。 For example, if user adds 2 triangles and 1 circle and saves, he can return to that session or start new one. 例如,如果用户添加2个三角形和1个圆并保存,则他可以返回该会话或开始新的会话。
I was thinking of saving the data to db for persistence. 我当时正在考虑将数据保存到db以获得持久性。
The problem is that I can't implement ability to save state without saving it to image. 问题是,如果不将状态保存到图像中,就无法实现保存状态的功能。
I don't want to save it to image because I want the user to continue working on the same session and able to remove or add more figures to it. 我不想将其保存到图像,因为我希望用户继续在同一会话上工作,并能够删除或添加更多图形。
Any help? 有什么帮助吗?
Thanks, Al 谢谢Al
A possible solution could me to store the coordinates / properties of the shapes in a object. 一个可能的解决方案是将形状的坐标/属性存储在对象中。 For example: 例如:
var shapes = [
[
// a rectangle
[10, 10],
[40, 10],
[40, 40],
[10, 40]
],
[
// a triangle
[100, 20],
[200, 100],
[0, 100]
]
];
Which is an array with all the shapes. 这是具有所有形状的数组。 And per shape, and array with [x, y]
coordinates. 以及每个形状和具有[x, y]
坐标的数组。 You could easily convert it to JSON to store in your database. 您可以轻松地将其转换为JSON以存储在数据库中。
But a circle would be more complicated, since it's made up of endless points. 但是一个圆会更加复杂,因为它由无数个点组成。 In that case, a solution could be to store the type of shape: 在这种情况下,解决方案可能是存储形状的类型:
var shapes = [
{
type: 'rectangle',
properties: {
x: 10,
y: 10,
width: 100,
height: 100
}
},
{
type: 'arc',
properties: {
x: 200,
y: 100,
radius: 50
}
},
{
type: 'custom', // triangle
properties: {
coords: [
[100, 20],
[200, 100],
[0, 100]
]
}
}
];
With the 2nd solution you would be able to store more stuff, like strokeColor, fill, etc. 使用第二种解决方案,您将能够存储更多的东西,例如笔触颜色,填充等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.