gpt4 book ai didi

jquery - 签名未在 html5 中的 Canvas 元素上显示

转载 作者:行者123 更新时间:2023-11-28 10:48:53 25 4
gpt4 key购买 nike

我正在尝试在 Canvas 上绘制签名,但签名没有显示。我需要 Canvas 适合 100% 宽度并且签名需要在不拉伸(stretch)的情况下出现。这是代码。

这是我的代码:

HTML:

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile 1.1.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.3.2.min.css" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<div data-role="page">
<div id="divcontent" data-role="content">
<table border='0' width="100%" >
<tr>
<td style='background-color:blue'>
<div id="sketch">
<canvas id="paint" style='width:100%;height:90px;background- color:yellow'></canvas>
</div>
</td>
</tr>
</table>
</div>

Js:

<script>
(function() {
var canvas = document.querySelector('#paint');
var ctx = canvas.getContext('2d');

var sketch = document.querySelector('#sketch');
var sketch_style = getComputedStyle(sketch);
canvas.width = parseInt(sketch_style.getPropertyValue('width'));
canvas.height = parseInt(sketch_style.getPropertyValue('height'));

var mouse = {x: 0, y: 0};
var last_mouse = {x: 0, y: 0};

/* Mouse Capturing Work */
canvas.addEventListener('mousemove', function(e) {
last_mouse.x = mouse.x;
last_mouse.y = mouse.y;
//alert("call");
console.log(mouse.x);
mouse.x = e.pageX - this.offsetLeft-6;
mouse.y = e.pageY - this.offsetTop-356;
console.log("muse y:"+mouse.y);
}, false);


/* Drawing on Paint App */
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.strokeStyle = 'red';

canvas.addEventListener('mousedown', function(e) {
canvas.addEventListener('mousemove', onPaint, false);
}, false);

canvas.addEventListener('mouseup', function() {
canvas.removeEventListener('mousemove', onPaint, false);
}, false);

var onPaint = function() {
console.log("paint");
console.log("last_mouse.x"+last_mouse.x);
console.log("last_mouse.y"+last_mouse.y);
ctx.beginPath();

ctx.moveTo(last_mouse.x, last_mouse.y);
ctx.lineTo(mouse.x, mouse.y);
console.log("paint mouse.x"+mouse.x);
console.log("paint mouse.y"+mouse.y);
ctx.closePath();
ctx.strokeStyle="red";
ctx.stroke();
};

}());

</script>

如何解决此问题以绘制签名。

最佳答案

首先从

下载一个用于在 Canvas 上绘图的启用触摸的 jquery 插件

https://github.com/jimdoescode/jqScribble

它适用于桌面浏览器和移动设备。

在你的html文件中添加以下脚本

<script src="js/jquery.jqscribble.js" type="text/javascript"></script>
<script src="js/jqscribble.extrabrushes.js" type="text/javascript"></script>

HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Signature demo</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<script src="js/jquery.jqscribble.js" type="text/javascript"></script>
<script src="js/jqscribble.extrabrushes.js" type="text/javascript"></script>
</head>
<body>
<div data-role="page" id="page_id" >
<div data-role="header" data-position="fixed">
</div>
<div role="main" class="ui-content" style="margin-top:60px;" >
<div class="heading">CREATE SIGNATURE HERE</div>
<canvas id="canvas" style="border: 1px solid red;"></canvas>
<input type="button" data-ajax="false" onclick='save();' class="ui-btn" value="Accept" />
<input type="button" data-ajax="false" onclick='$("#canvas").data("jqScribble").clear();' class="ui-btn" value="Resign" />
</div>
</div>
</body>
</html>

JavaScript

function save()
{

$("#canvas").data("jqScribble").save(function(imageData)
{
if(confirm("Are you sure want to save a signature image?"))
{
$.post('your_sign.php', {imagedata: imageData}, function(response)
{
alert(imageData)

});

}else{
return;
}
});
}

$(document).ready(function()
{
$("#canvas").jqScribble();
$(document).bind('touchmove', false);
});

关于jquery - 签名未在 html5 中的 Canvas 元素上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22861696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com