|
@@ -9,28 +9,48 @@ function EasyCanvas(option) {
|
|
this.canvas = option.canvas
|
|
this.canvas = option.canvas
|
|
this.context = this.canvas.getContext("2d")
|
|
this.context = this.canvas.getContext("2d")
|
|
this.ratio = this.getPixelRatio()
|
|
this.ratio = this.getPixelRatio()
|
|
- this.originSize = {}
|
|
|
|
|
|
|
|
- if (option.maxWidth != undefined && option.maxWidth > 0) {
|
|
|
|
- this.maxWidth = option.maxWidth
|
|
|
|
- } else if (option.maxHeight != undefined && option.maxHeight > 0) {
|
|
|
|
- this.maxHeight = option.maxHeight
|
|
|
|
|
|
+ this.originSize = {
|
|
|
|
+ width: 0,
|
|
|
|
+ height: 0
|
|
|
|
+ }
|
|
|
|
+ this.styleSize = {
|
|
|
|
+ width: 0,
|
|
|
|
+ height: 0
|
|
|
|
+ }
|
|
|
|
+ this.boxSize = {}
|
|
|
|
+ this.fontConfig = {
|
|
|
|
+ name: option.fontName || 'Arial',
|
|
|
|
+ size: option.fontSize || 60,
|
|
|
|
+ color: option.fontColor || 'red'
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
|
|
-EasyCanvas.prototype.setMaxWidth = function(width) {
|
|
|
|
- if (width != undefined) {
|
|
|
|
- this.maxWidth = width
|
|
|
|
- this.maxHeight = undefined
|
|
|
|
- this.onSizeChange()
|
|
|
|
|
|
+ if (option.boxWidth != undefined && boxWidth.boxWidth > 0) {
|
|
|
|
+ this.boxSize.width = option.boxWidth
|
|
|
|
+ } else if (option.boxHeight != undefined && option.boxHeight > 0) {
|
|
|
|
+ this.boxSize.height = option.boxHeight
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.onClick = option.onClick || (function() {})
|
|
|
|
+
|
|
|
|
+ let self = this
|
|
|
|
+ this.canvas.onclick = function(event) {
|
|
|
|
+ if (self.originSize.width > 0 && self.originSize.height > 0) {
|
|
|
|
+ self.onClick({
|
|
|
|
+ left: (event.offsetX - this.offsetLeft),
|
|
|
|
+ top: (event.offsetY - this.offsetTop)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-EasyCanvas.prototype.setMaxHeight = function(height) {
|
|
|
|
- if (height != undefined) {
|
|
|
|
- this.maxHeight = height
|
|
|
|
- this.maxWidth = undefined
|
|
|
|
- this.onSizeChange()
|
|
|
|
|
|
+EasyCanvas.prototype.changeBoxSize = function(width, height) {
|
|
|
|
+ if (width != undefined) {
|
|
|
|
+ this.boxSize.width = width
|
|
|
|
+ this.boxSize.height = undefined
|
|
|
|
+ } else if (height != undefined) {
|
|
|
|
+ this.boxSize.height = height
|
|
|
|
+ this.boxSize.width = undefined
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -48,80 +68,83 @@ EasyCanvas.prototype.clear = function() {
|
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.heigth)
|
|
this.context.clearRect(0, 0, this.canvas.width, this.canvas.heigth)
|
|
}
|
|
}
|
|
|
|
|
|
-EasyCanvas.prototype.resize = function(width, height) {
|
|
|
|
- this.originSize = {
|
|
|
|
- width: width || 0,
|
|
|
|
- height: height || 0
|
|
|
|
|
|
+EasyCanvas.prototype.reset = function(width, height) {
|
|
|
|
+ this.originSize.width = Math.max((width || 0), 0)
|
|
|
|
+ this.originSize.height = Math.max((height || 0), 0)
|
|
|
|
+ this.styleSize.width = this.originSize.width
|
|
|
|
+ this.styleSize.height = this.originSize.height
|
|
|
|
+
|
|
|
|
+ if (this.styleSize.width > 0 && this.styleSize.height > 0) {
|
|
|
|
+ if (this.boxSize.width != undefined) {
|
|
|
|
+ this.styleSize.width = Math.min(this.originSize.width, this.boxSize.width)
|
|
|
|
+ this.styleSize.height = this.styleSize.width * this.originSize.height / this.originSize.width
|
|
|
|
+ } else if (this.boxSize.height != undefined) {
|
|
|
|
+ this.styleSize.height = Math.min(this.originSize.height, this.boxSize.height)
|
|
|
|
+ this.styleSize.width = this.styleSize.height * this.originSize.width / this.originSize.height
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- this.canvas.width = this.originSize.width * this.ratio
|
|
|
|
- this.canvas.height = this.originSize.height * this.ratio
|
|
|
|
- this.context.scale(this.ratio, this.ratio)
|
|
|
|
|
|
+
|
|
|
|
+ this.canvas.width = this.styleSize.width * this.ratio
|
|
|
|
+ this.canvas.height = this.styleSize.height * this.ratio
|
|
|
|
+ this.canvas.style.width = this.styleSize.width + 'px'
|
|
|
|
+ this.canvas.style.height = this.styleSize.height + 'px'
|
|
|
|
+
|
|
this.clear()
|
|
this.clear()
|
|
- this.onSizeChange()
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-EasyCanvas.prototype.onSizeChange = function() {
|
|
|
|
- let width, height
|
|
|
|
- if (this.originSize.width == 0 || this.originSize.height == 0) {
|
|
|
|
- width = 0
|
|
|
|
- height = 0
|
|
|
|
- } else if (this.maxWidth != undefined) {
|
|
|
|
- width = Math.min(this.originSize.width, this.maxWidth)
|
|
|
|
- height = width * this.originSize.heigth / this.originSize.width
|
|
|
|
- } else if (this.maxHeigth != undefined) {
|
|
|
|
- height = Math.min(this.originSize.height, this.maxHeight)
|
|
|
|
- width = width * this.originSize.heigth / this.originSize.width
|
|
|
|
- } else {
|
|
|
|
- width = this.originSize.width
|
|
|
|
- height = this.originSize.height
|
|
|
|
- }
|
|
|
|
- this.canvas.style.width = width + 'px'
|
|
|
|
- this.canvas.style.height = height + 'px'
|
|
|
|
|
|
+EasyCanvas.prototype.drawImage = function(image) {
|
|
|
|
+ this.drawImages([image], undefined, 1.0)
|
|
}
|
|
}
|
|
|
|
|
|
-EasyCanvas.prototype.drawImages = function(images, configs) {
|
|
|
|
- let indexSet = {}
|
|
|
|
- if (configs == undefined || configs.length == 0) {
|
|
|
|
|
|
+EasyCanvas.prototype.drawImages = function(images, config, scale) {
|
|
|
|
+ scale = scale || 1.0
|
|
|
|
+ if (config == undefined || config.length == 0) {
|
|
//不指定拼接配置时,默认按顺序显示所有图片
|
|
//不指定拼接配置时,默认按顺序显示所有图片
|
|
- configs = []
|
|
|
|
|
|
+ config = []
|
|
for (let i = 0; i < images.length; i++) {
|
|
for (let i = 0; i < images.length; i++) {
|
|
- configs.push({
|
|
|
|
|
|
+ config.push({
|
|
i: i + 1,
|
|
i: i + 1,
|
|
x: 0,
|
|
x: 0,
|
|
y: 0,
|
|
y: 0,
|
|
w: 0,
|
|
w: 0,
|
|
h: 0
|
|
h: 0
|
|
})
|
|
})
|
|
- indexSet[i] = true
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- for (let i = 0; i < configs.length; i++) {
|
|
|
|
- indexSet[configs[i].i - 1] = true;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let maxWidth = 0;
|
|
let maxWidth = 0;
|
|
let totalHeight = 0;
|
|
let totalHeight = 0;
|
|
- for (let i = 0; i < configs.length; i++) {
|
|
|
|
|
|
+ for (let i = 0; i < config.length; i++) {
|
|
//计算最大宽度与合计高度
|
|
//计算最大宽度与合计高度
|
|
- if (configs[i].w <= 0) {
|
|
|
|
- configs[i].w = images[configs[i].i - 1].width;
|
|
|
|
|
|
+ if (config[i].w <= 0) {
|
|
|
|
+ config[i].w = images[config[i].i - 1].width;
|
|
}
|
|
}
|
|
- if (configs[i].h <= 0) {
|
|
|
|
- configs[i].h = images[configs[i].i - 1].height;
|
|
|
|
|
|
+ if (config[i].h <= 0) {
|
|
|
|
+ config[i].h = images[config[i].i - 1].height;
|
|
}
|
|
}
|
|
- maxWidth = Math.max(maxWidth, configs[i].w);
|
|
|
|
- totalHeight += configs[i].h;
|
|
|
|
|
|
+ maxWidth = Math.max(maxWidth, config[i].w * scale);
|
|
|
|
+ totalHeight += config[i].h * scale;
|
|
}
|
|
}
|
|
if (maxWidth > 0 && totalHeight > 0) {
|
|
if (maxWidth > 0 && totalHeight > 0) {
|
|
//设置画布大小
|
|
//设置画布大小
|
|
- this.resize(maxWidth, totalHeight)
|
|
|
|
|
|
+ this.reset(maxWidth, totalHeight)
|
|
//绘画到画布
|
|
//绘画到画布
|
|
|
|
+ scale = scale * this.styleSize.width / this.originSize.width
|
|
let height = 0;
|
|
let height = 0;
|
|
- for (let i = 0; i < configs.length; i++) {
|
|
|
|
- this.context.drawImage(images[configs[i].i - 1], configs[i].x, configs[i].y, configs[i].w, configs[i].h, 0, height, configs[i].w, configs[i].h);
|
|
|
|
- height += configs[i].h;
|
|
|
|
|
|
+ for (let i = 0; i < config.length; i++) {
|
|
|
|
+ this.context.drawImage(images[config[i].i - 1], config[i].x, config[i].y, config[i].w, config[i].h, 0, height, config[i].w * scale * this.ratio, config[i].h * scale * this.ratio);
|
|
|
|
+ height += (config[i].h * scale * this.ratio);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- this.resize()
|
|
|
|
|
|
+ this.reset()
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+EasyCanvas.prototype.drawText = function(text, left, top) {
|
|
|
|
+ this.context.font = this.fontConfig.size * this.ratio + 'px ' + this.fontConfig.name
|
|
|
|
+ this.context.fillStyle = this.fontConfig.color
|
|
|
|
+ this.context.fillText(text, left * this.ratio, top * this.ratio)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+EasyCanvas.prototype.getImageData = function(format) {
|
|
|
|
+ return this.canvas.toDataUrl(format ? format : 'image/jpeg')
|
|
}
|
|
}
|