gpt4 book ai didi

javascript - 扩展 javascript 以使用多个 Canvas 框

转载 作者:行者123 更新时间:2023-11-28 04:08:07 26 4
gpt4 key购买 nike

遗憾的是,我对 javascript 知之甚少。我现在正在写剧本。我想在线签署文件,可以使用任何类型的触摸屏。我有一个在线脚本,它提供了一个带有签名功能的小 Canvas 框。遗憾的是我的 JavaScript 技能太差,无法按照我想要的方式使用它。

var isSign = false;
var leftMButtonDown = false;

jQuery(function(){
//Initialize sign pad
init_Sign_Canvas();
});

function fun_submit() {
if(isSign) {
var canvas = $("#canvas").get(0);
var imgData = canvas.toDataURL();
jQuery('#page').find('p').remove();
jQuery('#page').find('img').remove();
jQuery('#page').append(jQuery('<p>Your Sign:</p>'));
jQuery('#page').append($('<img/>').attr('src',imgData));

closePopUp();
} else {
alert('Please sign');
}
}

function closePopUp() {
jQuery('#divPopUpSignContract').popup('close');
jQuery('#divPopUpSignContract').popup('close');
}

function init_Sign_Canvas() {
isSign = false;
leftMButtonDown = false;

//Set Canvas width
var sizedWindowWidth = $(window).width();
if(sizedWindowWidth > 700)
sizedWindowWidth = $(window).width() / 2;
else if(sizedWindowWidth > 400)
sizedWindowWidth = sizedWindowWidth - 100;
else
sizedWindowWidth = sizedWindowWidth - 50;

$("#canvas").width(200);
$("#canvas").height(50);
$("#canvas").css("border","1px solid #000");

var canvas = $("#canvas").get(0);

canvasContext = canvas.getContext('2d');

if(canvasContext)
{
canvasContext.canvas.width = 200;
canvasContext.canvas.height = 50;

canvasContext.fillStyle = "#fff";
canvasContext.fillRect(0,0,sizedWindowWidth,200);

canvasContext.moveTo(50,150);
canvasContext.lineTo(sizedWindowWidth-50,150);
canvasContext.stroke();

canvasContext.fillStyle = "#000";
canvasContext.font="20px Arial";
canvasContext.fillText("x",40,155);
}
// Bind Mouse events
$(canvas).on('mousedown', function (e) {
if(e.which === 1) {
leftMButtonDown = true;
canvasContext.fillStyle = "#000";
var x = e.pageX - $(e.target).offset().left;
var y = e.pageY - $(e.target).offset().top;
canvasContext.moveTo(x, y);
}
e.preventDefault();
return false;
});

$(canvas).on('mouseup', function (e) {
if(leftMButtonDown && e.which === 1) {
leftMButtonDown = false;
isSign = true;
}
e.preventDefault();
return false;
});

// draw a line from the last point to this one
$(canvas).on('mousemove', function (e) {
if(leftMButtonDown == true) {
canvasContext.fillStyle = "#000";
var x = e.pageX - $(e.target).offset().left;
var y = e.pageY - $(e.target).offset().top;
canvasContext.lineTo(x,y);
canvasContext.stroke();
}
e.preventDefault();
return false;
});

//bind touch events
$(canvas).on('touchstart', function (e) {
leftMButtonDown = true;
canvasContext.fillStyle = "#000";
var t = e.originalEvent.touches[0];
var x = t.pageX - $(e.target).offset().left;
var y = t.pageY - $(e.target).offset().top;
canvasContext.moveTo(x, y);

e.preventDefault();
return false;
});

$(canvas).on('touchmove', function (e) {
canvasContext.fillStyle = "#000";
var t = e.originalEvent.touches[0];
var x = t.pageX - $(e.target).offset().left;
var y = t.pageY - $(e.target).offset().top;
canvasContext.lineTo(x,y);
canvasContext.stroke();

e.preventDefault();
return false;
});

$(canvas).on('touchend', function (e) {
if(leftMButtonDown) {
leftMButtonDown = false;
isSign = true;
}

});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas">Canvas is not supported</canvas>

我需要 3 个 Canvas 框,可以相互平行地签名。是否可以给我一个提示或教程或其他东西,让我明白我需要改变什么?

最佳答案

不太确定这是否是您的意思(也许是三个独立的登录框?)。

使用 3 HTML5 <canvas>元素,并初始化它们。然后使用drawImage()函数将第一个 Canvas 图像复制到其他两个 Canvas 图像的 Canvas 上下文中。

var isSign = false;
var leftMButtonDown = false;

jQuery(function(){
//Initialize sign pad
init_Sign_Canvas();
});

function fun_submit() {
if(isSign) {
var canvas = $("#canvas").get(0);
var imgData = canvas.toDataURL();
jQuery('#page').find('p').remove();
jQuery('#page').find('img').remove();
jQuery('#page').append(jQuery('<p>Your Sign:</p>'));
jQuery('#page').append($('<img/>').attr('src',imgData));

closePopUp();
} else {
alert('Please sign');
}
}

function closePopUp() {
jQuery('#divPopUpSignContract').popup('close');
jQuery('#divPopUpSignContract').popup('close');
}

function init_Sign_Canvas() {
isSign = false;
leftMButtonDown = false;

//Set Canvas width
var sizedWindowWidth = $(window).width();
if(sizedWindowWidth > 700)
sizedWindowWidth = $(window).width() / 2;
else if(sizedWindowWidth > 400)
sizedWindowWidth = sizedWindowWidth - 100;
else
sizedWindowWidth = sizedWindowWidth - 50;

$("#canvas").width(200);
$("#canvas").height(50);
$("#canvas").css("border","1px solid #000");
$("#canvas2").width(200);
$("#canvas2").height(50);
$("#canvas2").css("border","1px solid #000");
$("#canvas3").width(200);
$("#canvas3").height(50);
$("#canvas3").css("border","1px solid #000");

var canvas = $("#canvas").get(0);
var canvas2 = $("#canvas2").get(0);
var canvas3 = $("#canvas3").get(0);

canvasContext = canvas.getContext('2d');
canvas2ctx = canvas2.getContext('2d');
canvas3ctx = canvas3.getContext('2d');
if(canvasContext)
{
canvasContext.canvas.width = 200;
canvasContext.canvas.height = 50;
canvas2ctx.canvas.width = 200;
canvas2ctx.canvas.height = 50;
canvas3ctx.canvas.width = 200;
canvas3ctx.canvas.height = 50;
canvasContext.fillStyle = "#fff";
canvasContext.fillRect(0,0,sizedWindowWidth,200);

canvasContext.moveTo(50,150);
canvasContext.lineTo(sizedWindowWidth-50,150);
canvasContext.stroke();

canvasContext.fillStyle = "#000";
canvasContext.font="20px Arial";
canvasContext.fillText("x",40,155);
}
// Bind Mouse events
$(canvas).on('mousedown', function (e) {
if(e.which === 1) {
leftMButtonDown = true;
canvasContext.fillStyle = "#000";
var x = e.pageX - $(e.target).offset().left;
var y = e.pageY - $(e.target).offset().top;
canvasContext.moveTo(x, y);
}
e.preventDefault();
return false;
});

$(canvas).on('mouseup', function (e) {
if(leftMButtonDown && e.which === 1) {
leftMButtonDown = false;
isSign = true;
canvas2ctx.drawImage(canvas,0,0);
canvas3ctx.drawImage(canvas,0,0);
}
e.preventDefault();
return false;
});

// draw a line from the last point to this one
$(canvas).on('mousemove', function (e) {
if(leftMButtonDown == true) {
canvasContext.fillStyle = "#000";
var x = e.pageX - $(e.target).offset().left;
var y = e.pageY - $(e.target).offset().top;
canvasContext.lineTo(x,y);
canvasContext.stroke();
}
e.preventDefault();
return false;
});

//bind touch events
$(canvas).on('touchstart', function (e) {
leftMButtonDown = true;
canvasContext.fillStyle = "#000";
var t = e.originalEvent.touches[0];
var x = t.pageX - $(e.target).offset().left;
var y = t.pageY - $(e.target).offset().top;
canvasContext.moveTo(x, y);

e.preventDefault();
return false;
});

$(canvas).on('touchmove', function (e) {
canvasContext.fillStyle = "#000";
var t = e.originalEvent.touches[0];
var x = t.pageX - $(e.target).offset().left;
var y = t.pageY - $(e.target).offset().top;
canvasContext.lineTo(x,y);
canvasContext.stroke();

e.preventDefault();
return false;
});

$(canvas).on('touchend', function (e) {
if(leftMButtonDown) {
leftMButtonDown = false;
isSign = true;
}

});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas">Canvas is not supported</canvas>
<canvas id="canvas2">Canvas is not supported</canvas>
<canvas id="canvas3">Canvas is not supported</canvas>

关于javascript - 扩展 javascript 以使用多个 Canvas 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46522719/

26 4 0
文章推荐: html - Chrome 和 Opera 占位符
文章推荐: javascript - 使用 Javascript 定位
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com