You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
9.8 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

(function (window, document, $) {
'use strict';
// Get a regular interval for drawing to the screen
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimaitonFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
/*
* Plugin Constructor
*/
var pluginName = 'jqSignature',
defaults = {
lineColor: '#222222',
lineWidth: 1,
border: '1px dashed #AAAAAA',
background: '#FFFFFF',
width: 300,
height: 100,
autoFit: false
},
canvasFixture = '<canvas></canvas>';
function Signature(element, options) {
// DOM elements/objects
this.element = element;
this.$element = $(this.element);
this.canvas = false;
this.$canvas = false;
this.ctx = false;
// Drawing state
this.drawing = false;
this.currentPos = {
x: 0,
y: 0
};
this.lastPos = this.currentPos;
// Determine plugin settings
this._data = this.$element.data();
this.settings = $.extend({}, defaults, options, this._data);
// Initialize the plugin
this.init();
}
Signature.prototype = {
// Initialize the signature canvas
init: function () {
// Set up the canvas
this.$canvas = $(canvasFixture).appendTo(this.$element);
this.$canvas.attr({
width: this.settings.width,
height: this.settings.height
});
this.$canvas.css({
boxSizing: 'border-box',
width: this.settings.width + 'px',
height: this.settings.height + 'px',
border: this.settings.border,
background: this.settings.background,
cursor: 'crosshair'
});
// Fit canvas to width of parent
if (this.settings.autoFit === true) {
this._resizeCanvas();
// TO-DO - allow for dynamic canvas resizing
// (need to save canvas state before changing width to avoid getting cleared)
// var timeout = false;
// $(window).on('resize', $.proxy(function(e) {
// clearTimeout(timeout);
// timeout = setTimeout($.proxy(this._resizeCanvas, this), 250);
// }, this));
}
this.canvas = this.$canvas[0];
this.cxt = this.canvas.getContext('2d');
this.x = []; //<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ǵ<EFBFBD>X<EFBFBD><58><EFBFBD><EFBFBD>
this.y = []; //<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ǵ<EFBFBD>Y<EFBFBD><59><EFBFBD><EFBFBD>
this.clickDrag = [];
this.lock = false; //<2F><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
this.storageColor = "#000000";
this.cxt.lineJoin = "round"; //context.lineJoin - ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶ε<DFB6><CEB5><EFBFBD><EFBFBD>ӷ<EFBFBD>ʽ
this.cxt.lineWidth1 = 5; //<2F><><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
var user = new WebUser();
console.log(user.Name);
if (user.Name == "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>") {
this.cxt.lineWidth1 = 2; //<2F><><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>
$("#select-font-size").val("2");
}
this.w = this.canvas.width; //ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>
this.h = this.canvas.height; //ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>
this.touch = ("createTouch" in document); //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD>ֳ<EFBFBD><D6B3>
this.StartEvent = this.touch ? "touchstart" : "mousedown"; //֧<>ִ<EFBFBD><D6B4><EFBFBD>ʽʹ<CABD><CAB9><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>
this.MoveEvent = this.touch ? "touchmove" : "mousemove";
this.EndEvent = this.touch ? "touchend" : "mouseup";
this._resetCanvas();
// Set up mouse events
this.$canvas.on('mousedown touchstart', $.proxy(function (e) {
//var touch = this.touch ? e.touches[0] : e;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƥ<EFBFBD><C6A4>
var skno1 = $('#clr').attr('val');
var xPos, yPos, rect;
rect = this.canvas.getBoundingClientRect();
var event = e;
if (event.type.indexOf('touch') !== -1) { // event.constructor === TouchEvent
xPos = event.touches[0].clientX - rect.left;
yPos = event.touches[0].clientY - rect.top;
}
// Mouse event
else {
xPos = event.clientX - rect.left;
yPos = event.clientY - rect.top;
}
var _x = xPos; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD>ϵ<EFBFBD>x<EFBFBD><78><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
var _y = yPos; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD>ϵ<EFBFBD>y<EFBFBD><79><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
if (skno1 == 1) {
this.resetEraser(_x, _y);
} else {
this.cxt.strokeStyle = "#000000";
this.cxt.lineWidth = this.cxt.lineWidth1;
/*
this.drawing = true;
this.lastPos = this.currentPos = this._getPosition(e);
*/
this.movePoint(_x, _y); //<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
this.drawPoint(); //<2F><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
}
this.lock = true;
}, this));
this.$canvas.on('mousemove touchmove', $.proxy(function (e) {
//var touch = this.touch ? e.touches[0] : e;
var skno1 = $('#clr').attr('val');
if (this.lock) { //t.lockΪtrue<75><65>ִ<EFBFBD><D6B4>
var xPos, yPos, rect;
rect = this.canvas.getBoundingClientRect();
var event = e;
if (event.type.indexOf('touch') !== -1) { // event.constructor === TouchEvent
xPos = event.touches[0].clientX - rect.left;
yPos = event.touches[0].clientY - rect.top;
}
// Mouse event
else {
xPos = event.clientX - rect.left;
yPos = event.clientY - rect.top;
}
var _x = xPos; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD>ϵ<EFBFBD>x<EFBFBD><78><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>
var _y = yPos; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><DABB><EFBFBD><EFBFBD>ϵ<EFBFBD>y<EFBFBD><79><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> {
if (skno1 == 1) {
this.resetEraser(_x, _y);
} else {
this.cxt.strokeStyle = "#000000";
this.cxt.lineWidth = this.cxt.lineWidth1;
/*
this.currentPos = this._getPosition(e);
*/
this.movePoint(_x, _y, true); //<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
this.drawPoint(); //<2F><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
}
}
}, this));
this.$canvas.on('mouseup touchend', $.proxy(function (e) {
this.lock = false;
this.x = [];
this.y = [];
this.clickDrag = [];
this.drawing = false;
// Trigger a change event
var changedEvent = $.Event('jq.signature.changed');
this.$element.trigger(changedEvent);
}, this));
// Prevent document scrolling when touching canvas
$(document).on('mouseup touchend', $.proxy(function (e) {
this.lock = false;
this.x = [];
this.y = [];
this.clickDrag = [];
this.drawing = false;
// Trigger a change event
var changedEvent = $.Event('jq.signature.changed');
this.$element.trigger(changedEvent);
}, this));
$(document).on('touchstart touchmove touchend', $.proxy(function (e) {
if (e.target === this.canvas) {
e.preventDefault();
}
}, this));
// Start drawing
var that = this;
(function drawLoop() {
window.requestAnimFrame(drawLoop);
that._renderCanvas();
})();
},
// Clear the canvas
clearCanvas: function () {
this.canvas.width = this.canvas.width;
this._resetCanvas();
},
// Get the content of the canvas as a base64 data URL
getDataURL: function () {
return this.canvas.toDataURL();
},
setfont: function () { //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
var fontsize = $("#select-font-size").val();
console.log(fontsize);
this.cxt.lineWidth1 = parseInt(fontsize);
},
// Get the position of the mouse/touch
_getPosition: function (event) {
var xPos, yPos, rect;
rect = this.canvas.getBoundingClientRect();
event = event.originalEvent;
// Touch event
if (event.type.indexOf('touch') !== -1) { // event.constructor === TouchEvent
xPos = event.touches[0].clientX - rect.left;
yPos = event.touches[0].clientY - rect.top;
}
// Mouse event
else {
xPos = event.clientX - rect.left;
yPos = event.clientY - rect.top;
}
return {
x: xPos,
y: yPos
};
},
movePoint: function (x, y, dragging) {
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>Զ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
this.x.push(x);
this.y.push(y);
this.clickDrag.push(y);
},
drawPoint: function (x, y, radius) {
for (var i = 0; i < this.x.length; i++) //ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
this.cxt.beginPath(); //context.beginPath() , ׼<><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>·<EFBFBD><C2B7>
if (this.clickDrag[i] && i) { //<2F><><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD>i!=0ʱ<30><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ʼ<E3BFAA><CABC><EFBFBD>ߡ<EFBFBD>
this.cxt.moveTo(this.x[i - 1], this.y[i - 1]); //context.moveTo(x, y) , <20>¿<EFBFBD>һ<EFBFBD><D2BB>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
} else {
this.cxt.moveTo(this.x[i] - 1, this.y[i]);
}
this.cxt.lineTo(this.x[i], this.y[i]); //context.lineTo(x, y) , <20><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
this.cxt.closePath(); //context.closePath() , <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ·<C7B0><C2B7><EFBFBD>Ǵ򿪵<C7B4><F2BFAAB5><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD>
this.cxt.stroke(); //context.stroke() , <20><><EFBFBD>Ƶ<EFBFBD>ǰ·<C7B0><C2B7>
}
},
resetEraser: function (_x, _y) {
/*ʹ<><CAB9><EFBFBD><EFBFBD>Ƥ<EFBFBD><C6A4>-<2D><><EFBFBD><EFBFBD>*/
var t = this;
//this.cxt.lineWidth = 30;
/*source-over Ĭ<><C4AC>,<2C><EFBFBD><E0BDBB><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD>(<28><>ɫ,<2C><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>)<29><><EFBFBD><EFBFBD><><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>*/
t.cxt.globalCompositeOperation = "destination-out";
t.cxt.beginPath();
t.cxt.arc(_x, _y, this.cxt.lineWidth1 + 6, 0, Math.PI * 2);
t.cxt.strokeStyle = "rgba(250,250,250,0)";
t.cxt.fill();
t.cxt.globalCompositeOperation = "source-over"
},
// Render the signature to the canvas
_renderCanvas: function () {
if (this.drawing) {
this.ctx.moveTo(this.lastPos.x, this.lastPos.y);
this.ctx.lineTo(this.currentPos.x, this.currentPos.y);
this.ctx.stroke();
this.lastPos = this.currentPos;
}
},
// Reset the canvas context
_resetCanvas: function () {
//this.ctx = this.canvas.getContext("2d");
// this.ctx.strokeStyle = "#000000";
//this.ctx.lineWidth = 5;
},
// Resize the canvas element
_resizeCanvas: function () {
var width = this.$element.outerWidth();
this.$canvas.attr('width', width);
this.$canvas.css('width', width + 'px');
}
};
/*
* Plugin wrapper and initialization
*/
$.fn[pluginName] = function (options) {
var args = arguments;
if (options === undefined || typeof options === 'object') {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Signature(this, options));
}
});
} else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
var returns;
this.each(function () {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Signature && typeof instance[options] === 'function') {
returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
}
if (options === 'destroy') {
$.data(this, 'plugin_' + pluginName, null);
}
});
return returns !== undefined ? returns : this;
}
};
})(window, document, jQuery);