【发布时间】:2011-08-23 21:45:34
【问题描述】:
我这里有一些实现签名板的 javascript 代码。它在谷歌浏览器上运行良好,但在 Firefox 上不起作用。代码如下:
<html>
<head>
<title>Signature Pad</title>
<script src="lib/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//fires when document is loaded and DOM is ready.
$(document).ready(function() {
var context, canvas, drawLine, mouseIsPressed, x, y, prevX, prevY, borderWidth;
canvas = document.getElementById('canvas'); //retrieve canvas from dom by id that we have assigned
context = canvas.getContext('2d'); //retrieve context
borderWidth = 5; //let border width will be 5 pixel
canvas.style.border = borderWidth + " px solid #00F000"; // 5 pixel width solid green border
drawLine = function(x1, y1, x2, y2)
{
context.beginPath(); //create a path
context.moveTo(x1 - borderWidth, y1 - borderWidth); //move to
context.lineTo(x2 - borderWidth, y2 - borderWidth); //draw a line
context.stroke(); // filled with "ink"
context.closePath(); //close path
};
canvas.onmousedown = function(evt)
{
mouseIsPressed = true; //save that mouse is pressed
drawLine(evt.offsetX, evt.offsetY, evt.offsetX + 1, evt.offsetY + + 1) //draw short line that looks like a dot
};
canvas.onmouseup = function(evt)
{
mouseIsPressed = false; //save that mouse is released
};
canvas.onmousemove = function(evt)
{
x = evt.offsetX;
y = evt.offsetY; //get current X and Y
if(mouseIsPressed)
{
drawLine(prevX, prevY, x, y); //draw a line on canvas from previous to current point.
}
prevX = x; prevY = y; //save previous x and y in both case, weather mouse is pressed or not
};
});
</script>
<head>
<body>
<canvas width="300" height="200" id="canvas"></canvas>
</body>
</html>
据我所知,Firefox 确实支持 HTML5 Canvas。而且我确实有版本 6。我还检查了是否禁用了 javascript,但这不是问题。我不知道为什么它不起作用。
【问题讨论】:
-
控制台会显示什么?任何报告的错误?
-
你确定 Firefox 支持 HTML5 吗?我已经使用 Chrome 有一段时间了,所以我不知道 FF 在哪里——但我会仔细检查 FF(尤其是您安装的版本)是否支持 Canvas。编辑 - 好的,谷歌搜索显示确实如此,但请确保您的版本如此。
-
@transistor1: 为什么不先检查呢?如果谷歌太硬,还有caniuse.com/#search=canvas
-
@Wladimir Palant - 哎哟。如您所见,在您发布友好帖子之前,我确实检查过;但是 OP 仍然需要检查他/她的版本是否支持 Canvas,谢谢。
标签: javascript html canvas