【发布时间】:2014-10-08 10:48:55
【问题描述】:
我有一个大学项目来创建平面图设计软件。为此,我正在尝试在 HTML Canvas 和网格的帮助下创建平面图。我想将创建的交互式平面图保存到数据库中,但不能像将平面图保存在数据库中时一样,将其转换为图像文件。我想知道如何在创建地图后将交互式地图保存在数据库中,以及拿来做改动。
简单的canvas map代码如下..
<!doctype>
<html>
<head>
</head>
<body style=" background: lightblue;">
<canvas id="canvas" width="500px" height="500px"style="background: #fff; magrin:20px;">
Browser does not support canvas </canvas>
<img id="canvasImg" alt="Right click to save me!">
<script type="text/javascript" language="javascript">
var bw = 400;
var bh = 400;
var p = 10;
var cw = bw + (p*2) + 1;
var ch = bh + (p*2) + 1;
var grid = 50;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
function drawBoard(){
context.beginPath();
for (var x = 0; x <= bw; x += grid){
context.moveTo(0.5 + x + p, p);
context.lineTo(0.5 + x + p, bh + p);
}
for (var x = 0; x <= bh; x += grid) {
context.moveTo(p, 0.5 + x + p);
context.lineTo(bw + p, 0.5 + x + p);
}
context.lineWidth = 1;
context.strokeStyle = "black";
context.stroke();
}
drawBoard();
function drawRect() {
context.beginPath();
context.rect(0.5+p+5*grid, 0.5+p+3*grid, 2*grid, 3*grid);
context.rect(0.0+p+0*grid, 0.0+p+0*grid, 0*grid, 0*grid);
context.rect(0.5+p+3*grid, 0.5+p+3*grid, 2*grid, 3*grid);
context.rect(0.5+p+0*grid, 0.5+p+0*grid, 2*grid, 3*grid);
context.fillStyle = 'yellow';
context.fill();
context.lineWidth = 2;
context.strokeStyle = 'blue';
context.stroke();
}
drawRect();
////new
var el = document.getElementById('canvas');
var ctx = el.getContext('2d');
var isDrawing;
el.onmousedown = function(e) {
isDrawing = true;
ctx.moveTo(e.clientX, e.clientY);
};
el.onmousemove = function(e) {
if (isDrawing) {
ctx.lineTo(e.clientX, e.clientY);
ctx.stroke();
}
};
el.onmouseup = function() {
isDrawing = false;
};
}
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL
// so it can be saved as an image
document.getElementById('canvasImg').src = dataURL;
</script>
</body>
</html>
这是输出::
这是静态计划,
我想动态创建并将完整的计划存储在我的数据库(MySQL)上,这可能吗?给出充分的答案。
【问题讨论】:
-
我将采用的方法是创建一个单独的逻辑地图对象,您可以在其中保存有关坐标和尺寸的信息,并基于您在画布上绘制的对象。您也可以轻松地将这个对象保存到数据库中,因为它只是数据。
标签: javascript canvas html5-canvas