PainterCore/painter.js

775 lines
22 KiB
JavaScript
Raw Normal View History

2018-07-05 15:27:12 +08:00
import Pen from './lib/pen';
import Downloader from './lib/downloader';
2018-07-10 18:24:28 +08:00
const util = require('./lib/util');
2018-07-05 15:27:12 +08:00
const downloader = new Downloader();
// 最大尝试的绘制次数
const MAX_PAINT_COUNT = 5;
2019-11-22 09:45:29 +08:00
const ACTION_DEFAULT_SIZE = 24;
2019-11-20 11:36:31 +08:00
const ACTION_OFFSET = '2rpx';
2018-07-05 15:27:12 +08:00
Component({
canvasWidthInPx: 0,
canvasHeightInPx: 0,
paintCount: 0,
2019-11-13 10:56:24 +08:00
currentPalette: {},
2019-11-18 14:08:07 +08:00
movingCache: {},
2018-07-05 15:27:12 +08:00
/**
* 组件的属性列表
*/
properties: {
customStyle: {
type: String,
},
// 运行自定义选择框和删除缩放按钮
customActionStyle: {
type: Object,
},
2018-07-05 15:27:12 +08:00
palette: {
type: Object,
2019-11-21 19:41:37 +08:00
observer: function (newVal, oldVal) {
2018-07-05 15:27:12 +08:00
if (this.isNeedRefresh(newVal, oldVal)) {
this.paintCount = 0;
this.startPaint();
}
},
},
dancePalette: {
type: Object,
2019-11-21 19:41:37 +08:00
observer: function (newVal, oldVal) {
if (!this.isEmpty(newVal)) {
this.initDancePalette(newVal);
}
},
},
2019-11-06 17:20:44 +08:00
widthPixels: {
type: Number,
value: 0
},
// 启用脏检查,默认 false
dirty: {
type: Boolean,
2018-08-17 12:24:44 +08:00
value: false,
},
2019-11-26 11:08:07 +08:00
LRU: {
type: Boolean,
value: true,
},
2019-11-18 11:44:34 +08:00
action: {
2019-11-13 10:56:24 +08:00
type: Object,
2019-11-21 19:41:37 +08:00
observer: function (newVal, oldVal) {
2019-11-18 11:44:34 +08:00
if (newVal) {
this.doAction(newVal)
}
2019-11-13 10:56:24 +08:00
},
}
2018-07-05 15:27:12 +08:00
},
data: {
picURL: '',
showCanvas: true,
painterStyle: '',
},
methods: {
2018-07-05 15:27:12 +08:00
/**
* 判断一个 object 是否为
* @param {object} object
*/
isEmpty(object) {
for (const i in object) {
return false;
}
return true;
},
isNeedRefresh(newVal, oldVal) {
if (!newVal || this.isEmpty(newVal) || (this.data.dirty && util.equal(newVal, oldVal))) {
2018-07-05 15:27:12 +08:00
return false;
}
return true;
},
2019-11-21 19:41:37 +08:00
getBox(rect, type) {
const boxArea = {
type: 'rect',
css: {
height: `${rect.bottom - rect.top}px`,
width: `${rect.right - rect.left}px`,
left: `${rect.left}px`,
top: `${rect.top}px`,
2019-11-21 19:41:37 +08:00
borderWidth: '4rpx',
borderColor: '#1A7AF8',
color: 'transparent'
}
}
2019-11-21 19:41:37 +08:00
if (type === 'text') {
boxArea.css = Object.assign({}, boxArea.css, {
borderStyle: 'dashed'
})
}
if (this.properties.customActionStyle && this.properties.customActionStyle.border) {
boxArea.css = Object.assign({}, boxArea.css, this.properties.customActionStyle.border)
}
Object.assign(boxArea, {
id: 'box'
})
return boxArea
},
2019-11-20 11:36:31 +08:00
getScaleIcon(rect, type) {
let scaleArea = {}
2019-11-22 09:45:29 +08:00
const {
customActionStyle
} = this.properties
if (customActionStyle && customActionStyle.scale) {
scaleArea = {
type: 'image',
url: type === 'text' ? customActionStyle.scale.textIcon : customActionStyle.scale.imageIcon,
css: {
height: `${2 * ACTION_DEFAULT_SIZE}rpx`,
width: `${2 * ACTION_DEFAULT_SIZE}rpx`,
borderRadius: `${ACTION_DEFAULT_SIZE}rpx`,
}
}
} else {
scaleArea = {
type: 'rect',
css: {
2019-11-22 09:45:29 +08:00
height: `${2 * ACTION_DEFAULT_SIZE}rpx`,
width: `${2 * ACTION_DEFAULT_SIZE}rpx`,
borderRadius: `${ACTION_DEFAULT_SIZE}rpx`,
color: '#0000ff',
}
}
}
scaleArea.css = Object.assign({}, scaleArea.css, {
align: 'center',
2019-11-20 11:36:31 +08:00
left: `${rect.right + ACTION_OFFSET.toPx()}px`,
top: type === 'text' ? `${rect.top - ACTION_OFFSET.toPx() - scaleArea.css.height.toPx() / 2}px` : `${rect.bottom - ACTION_OFFSET.toPx() - scaleArea.css.height.toPx() / 2}px`
})
Object.assign(scaleArea, {
id: 'scale'
})
return scaleArea
},
getDeleteIcon(rect) {
let deleteArea = {}
2019-11-22 09:45:29 +08:00
const {
customActionStyle
} = this.properties
if (customActionStyle && customActionStyle.scale) {
deleteArea = {
type: 'image',
url: customActionStyle.delete.icon,
css: {
height: `${2 * ACTION_DEFAULT_SIZE}rpx`,
width: `${2 * ACTION_DEFAULT_SIZE}rpx`,
borderRadius: `${ACTION_DEFAULT_SIZE}rpx`,
}
}
} else {
deleteArea = {
type: 'rect',
css: {
2019-11-22 09:45:29 +08:00
height: `${2 * ACTION_DEFAULT_SIZE}rpx`,
width: `${2 * ACTION_DEFAULT_SIZE}rpx`,
borderRadius: `${ACTION_DEFAULT_SIZE}rpx`,
color: '#0000ff',
}
}
}
deleteArea.css = Object.assign({}, deleteArea.css, {
align: 'center',
2019-11-20 11:36:31 +08:00
left: `${rect.left - ACTION_OFFSET.toPx()}px`,
top: `${rect.top - ACTION_OFFSET.toPx() - deleteArea.css.height.toPx() / 2}px`
})
Object.assign(deleteArea, {
id: 'delete'
})
return deleteArea
},
doAction(action, callback, isMoving) {
let newVal = null
if (action) {
newVal = action.view
}
2019-11-18 11:44:34 +08:00
if (newVal && newVal.id && this.touchedView.id !== newVal.id) {
// 带 id 的动作给撤回时使用,不带 id表示对当前选中对象进行操作
const {
views
} = this.currentPalette;
for (let i = 0; i < views.length; i++) {
if (views[i].id === newVal.id) {
// 跨层回撤,需要重新构建三层关系
this.touchedView = views[i];
this.findedIndex = i;
this.sliceLayers();
break
}
}
}
const doView = this.touchedView
if (!doView) {
return
}
2019-11-13 18:28:46 +08:00
if (newVal && newVal.css) {
2019-11-18 11:44:34 +08:00
if (Array.isArray(doView.css) && Array.isArray(newVal.css)) {
doView.css = Object.assign({}, ...doView.css, ...newVal.css)
} else if (Array.isArray(doView.css)) {
doView.css = Object.assign({}, ...doView.css, newVal.css)
} else if (Array.isArray(newVal.css)) {
doView.css = Object.assign({}, doView.css, ...newVal.css)
2019-11-13 18:28:46 +08:00
} else {
2019-11-18 11:44:34 +08:00
doView.css = Object.assign({}, doView.css, newVal.css)
2019-11-13 10:56:24 +08:00
}
2019-11-13 18:28:46 +08:00
}
2019-11-26 14:20:12 +08:00
if (newVal && newVal.rect) {
2019-11-26 14:17:49 +08:00
doView.rect = newVal.rect;
}
2019-11-26 11:08:07 +08:00
if (newVal && newVal.url && doView.url && newVal.url !== doView.url) {
downloader.download(newVal.url, this.properties.LRU).then((path) => {
wx.getImageInfo({
src: path,
success: () => {
doView.originUrl = newVal.url
doView.url = path;
newVal.sHeight && (doView.sHeight = newVal.sHeight);
newVal.sWidth && (doView.sWidth = newVal.sWidth);
this.reDraw(doView, callback, isMoving)
},
})
2019-11-26 11:08:07 +08:00
})
2019-11-26 12:04:26 +08:00
} else {
(newVal && newVal.text && doView.text && newVal.text !== doView.text) && (doView.text = newVal.text);
(newVal && newVal.content && doView.content && newVal.content !== doView.content) && (doView.content = newVal.content);
this.reDraw(doView, callback, isMoving)
}
},
2019-11-25 14:51:56 +08:00
2019-11-26 12:04:26 +08:00
reDraw(doView, callback, isMoving) {
2019-11-13 18:28:46 +08:00
const draw = {
width: this.currentPalette.width,
height: this.currentPalette.height,
2019-11-18 14:08:07 +08:00
views: this.isEmpty(doView) ? [] : [doView]
2019-11-13 18:28:46 +08:00
}
2019-11-22 09:45:29 +08:00
2019-11-13 18:28:46 +08:00
const pen = new Pen(this.globalContext, draw);
if (isMoving && doView.type === 'text') {
2019-11-18 14:08:07 +08:00
pen.paint(callback, true, this.movingCache);
} else {
pen.paint(callback)
}
const {
2019-11-20 11:36:31 +08:00
rect,
css,
type
2019-11-18 11:44:34 +08:00
} = doView
this.block = {
width: this.currentPalette.width,
height: this.currentPalette.height,
2019-11-21 19:41:37 +08:00
views: this.isEmpty(doView) ? [] : [this.getBox(rect, doView.type)]
}
2019-11-20 11:36:31 +08:00
if (css && css.scalable) {
this.block.views.push(this.getScaleIcon(rect, type))
}
2019-11-20 11:36:31 +08:00
if (css && css.deletable) {
this.block.views.push(this.getDeleteIcon(rect))
2019-11-18 14:08:07 +08:00
}
const topBlock = new Pen(this.frontContext, this.block)
topBlock.paint();
},
isInView(x, y, rect) {
return (x > rect.left &&
y > rect.top &&
x < rect.right &&
y < rect.bottom
)
2019-11-13 10:56:24 +08:00
},
isInDelete(x, y) {
for (const view of this.block.views) {
if (view.id === 'delete') {
return (x > view.rect.left &&
y > view.rect.top &&
x < view.rect.right &&
y < view.rect.bottom)
}
}
return false
},
isInScale(x, y) {
for (const view of this.block.views) {
if (view.id === 'scale') {
return (x > view.rect.left &&
y > view.rect.top &&
x < view.rect.right &&
y < view.rect.bottom)
}
}
return false
2019-11-18 14:08:07 +08:00
},
2019-11-13 10:56:24 +08:00
touchedView: {},
findedIndex: -1,
2019-11-18 14:08:07 +08:00
onClick() {
const x = this.startX
const y = this.startY
2019-11-13 18:28:46 +08:00
const totalLayerCount = this.currentPalette.views.length
let canBeTouched = []
2019-11-18 14:08:07 +08:00
let isDelete = false
let deleteIndex = -1
2019-11-13 18:28:46 +08:00
for (let i = totalLayerCount - 1; i >= 0; i--) {
const view = this.currentPalette.views[i]
const {
rect
} = view
if (this.touchedView && this.touchedView.id && this.touchedView.id === view.id && this.isInDelete(x, y, rect)) {
2019-11-18 14:08:07 +08:00
canBeTouched.length = 0
deleteIndex = i
2019-11-18 14:08:07 +08:00
isDelete = true
break
}
if (this.isInView(x, y, rect)) {
canBeTouched.push({
view,
index: i
})
}
}
2019-11-18 14:08:07 +08:00
this.touchedView = {}
if (canBeTouched.length === 0) {
this.findedIndex = -1
} else {
let i = 0
const touchAble = canBeTouched.filter(item => Boolean(item.view.id))
if (touchAble.length === 0) {
this.findedIndex = canBeTouched[0].index
} else {
for (i = 0; i < touchAble.length; i++) {
if (this.findedIndex === touchAble[i].index) {
i++
break
}
2019-11-13 10:56:24 +08:00
}
if (i === touchAble.length) {
i = 0
}
this.touchedView = touchAble[i].view
this.findedIndex = touchAble[i].index
2019-11-28 14:02:21 +08:00
this.triggerEvent('viewClicked', {
2019-11-21 19:41:37 +08:00
view: this.touchedView
})
2019-11-13 10:56:24 +08:00
}
}
if (this.findedIndex < 0 || (this.touchedView && !this.touchedView.id)) {
// 证明点击了背景 或无法移动的view
const block = {
width: this.currentPalette.width,
height: this.currentPalette.height,
views: []
}
const topBlock = new Pen(this.frontContext, block)
topBlock.paint();
2019-11-18 14:08:07 +08:00
if (isDelete) {
this.triggerEvent('touchEnd', {
2019-11-19 10:03:44 +08:00
view: this.currentPalette.views[deleteIndex],
2019-11-19 10:28:07 +08:00
index: deleteIndex,
type: 'delete'
})
2019-11-18 14:08:07 +08:00
this.doAction()
} else if (this.findedIndex < 0) {
this.triggerEvent('touchStart', {})
}
this.findedIndex = -1
2019-11-18 11:44:34 +08:00
this.prevFindedIndex = -1
} else if (this.touchedView && this.touchedView.id) {
2019-11-18 11:44:34 +08:00
this.sliceLayers()
}
},
sliceLayers() {
const bottomLayers = this.currentPalette.views.slice(0, this.findedIndex)
const topLayers = this.currentPalette.views.slice(this.findedIndex + 1)
const bottomDraw = {
width: this.currentPalette.width,
height: this.currentPalette.height,
background: this.currentPalette.background,
views: bottomLayers
}
const topDraw = {
width: this.currentPalette.width,
height: this.currentPalette.height,
views: topLayers
}
if (this.prevFindedIndex < this.findedIndex) {
new Pen(this.bottomContext, bottomDraw).paint();
2019-11-18 14:08:07 +08:00
this.doAction(null, (callbackInfo) => {
this.movingCache = callbackInfo
})
2019-11-18 11:44:34 +08:00
new Pen(this.topContext, topDraw).paint();
} else {
new Pen(this.topContext, topDraw).paint();
2019-11-18 14:08:07 +08:00
this.doAction(null, (callbackInfo) => {
this.movingCache = callbackInfo
})
2019-11-18 11:44:34 +08:00
new Pen(this.bottomContext, bottomDraw).paint();
}
this.prevFindedIndex = this.findedIndex
},
2019-11-13 18:28:46 +08:00
startX: 0,
startY: 0,
startH: 0,
startW: 0,
isScale: false,
startTimeStamp: 0,
onTouchStart(event) {
const {
x,
y
} = event.touches[0]
this.startX = x
this.startY = y
this.startTimeStamp = new Date().getTime()
if (this.touchedView && !this.isEmpty(this.touchedView)) {
const {
rect
} = this.touchedView
if (this.isInScale(x, y, rect)) {
this.isScale = true
2019-11-18 14:08:07 +08:00
this.movingCache = {}
this.startH = rect.bottom - rect.top
this.startW = rect.right - rect.left
} else {
this.isScale = false
}
2019-11-13 18:28:46 +08:00
}
2019-11-13 10:56:24 +08:00
},
onTouchEnd(e) {
const current = new Date().getTime()
if ((current - this.startTimeStamp) <= 500 && !this.hasMove) {
!this.isScale && this.onClick(e)
} else if (this.touchedView && !this.isEmpty(this.touchedView)) {
2019-11-18 11:44:34 +08:00
this.triggerEvent('touchEnd', {
view: this.touchedView,
2019-11-18 11:44:34 +08:00
})
}
this.hasMove = false
2019-11-13 10:56:24 +08:00
},
onTouchCancel(e) {
this.onTouchEnd(e)
2019-11-13 10:56:24 +08:00
},
hasMove: false,
2019-11-13 10:56:24 +08:00
onTouchMove(event) {
this.hasMove = true
if (!this.touchedView || (this.touchedView && !this.touchedView.id)) {
2019-11-13 10:56:24 +08:00
return
}
const {
x,
y
} = event.touches[0]
const offsetX = x - this.startX
const offsetY = y - this.startY
const {
rect,
type
} = this.touchedView
let css = {}
if (this.isScale) {
const newW = this.startW + offsetX > 1 ? this.startW + offsetX : 1
2019-11-22 10:13:17 +08:00
if (this.touchedView.css && this.touchedView.css.minWidth) {
if (newW < this.touchedView.css.minWidth.toPx()) {
return
}
}
if (this.touchedView.rect && this.touchedView.rect.minWidth) {
if (newW < this.touchedView.rect.minWidth) {
return
}
}
const newH = this.startH + offsetY > 1 ? this.startH + offsetY : 1
css = {
width: `${newW}px`,
}
if (type !== 'text') {
if (type === 'image') {
css.height = `${(newW) * this.startH / this.startW }px`
} else {
css.height = `${newH}px`
}
}
} else {
this.startX = x
this.startY = y
css = {
left: `${rect.x + offsetX}px`,
top: `${rect.y + offsetY}px`,
right: undefined,
bottom: undefined
}
2019-11-13 10:56:24 +08:00
}
this.doAction({
view: {
css
}
2019-11-18 14:08:07 +08:00
}, (callbackInfo) => {
if (this.isScale) {
this.movingCache = callbackInfo
}
}, !this.isScale)
2019-11-13 10:56:24 +08:00
},
initScreenK() {
if (!(getApp() && getApp().systemInfo && getApp().systemInfo.screenWidth)) {
2018-07-05 15:27:12 +08:00
try {
getApp().systemInfo = wx.getSystemInfoSync();
2018-07-05 15:27:12 +08:00
} catch (e) {
console.error(`Painter get system info failed, ${JSON.stringify(e)}`);
2018-07-10 18:24:28 +08:00
return;
2018-07-05 15:27:12 +08:00
}
}
this.screenK = 0.5;
if (getApp() && getApp().systemInfo && getApp().systemInfo.screenWidth) {
this.screenK = getApp().systemInfo.screenWidth / 750;
}
setStringPrototype(this.screenK, 1);
},
2019-11-13 10:56:24 +08:00
initDancePalette() {
this.initScreenK();
this.downloadImages(this.properties.dancePalette).then((palette) => {
2019-11-13 10:56:24 +08:00
this.currentPalette = palette
2019-11-06 17:20:44 +08:00
const {
width,
height
} = palette;
2019-11-13 10:56:24 +08:00
2018-07-05 15:27:12 +08:00
if (!width || !height) {
console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`);
return;
}
this.setData({
painterStyle: `width:${width.toPx()}px;height:${height.toPx()}px;`,
});
2019-11-18 11:44:34 +08:00
this.frontContext || (this.frontContext = wx.createCanvasContext('front', this));
this.bottomContext || (this.bottomContext = wx.createCanvasContext('bottom', this));
this.topContext || (this.topContext = wx.createCanvasContext('top', this));
this.globalContext || (this.globalContext = wx.createCanvasContext('k-canvas', this));
2019-11-28 10:32:20 +08:00
new Pen(this.bottomContext, palette).paint();
new Pen(this.globalContext, {
width,
height,
views: []
2019-11-28 10:32:20 +08:00
}).paint();
new Pen(this.frontContext, {
2019-11-19 10:28:07 +08:00
width,
height,
views: []
2019-11-28 10:32:20 +08:00
}).paint();
2019-11-19 10:28:07 +08:00
new Pen(this.topContext, {
width,
height,
views: []
2019-11-28 10:32:20 +08:00
}).paint();
});
},
startPaint() {
this.initScreenK();
this.downloadImages(this.properties.palette).then((palette) => {
const {
width,
height
} = palette;
if (!width || !height) {
console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`);
return;
}
// 生成图片时,根据设置的像素值重新绘制
2019-11-06 17:20:44 +08:00
this.canvasWidthInPx = width.toPx();
if (this.properties.widthPixels) {
setStringPrototype(this.screenK, this.properties.widthPixels / this.canvasWidthInPx)
2019-11-06 17:20:44 +08:00
this.canvasWidthInPx = this.properties.widthPixels
}
2019-11-13 10:56:24 +08:00
2019-11-06 17:20:44 +08:00
this.canvasHeightInPx = height.toPx();
2018-07-05 15:27:12 +08:00
this.setData({
photoStyle: `width:${this.canvasWidthInPx}px;height:${this.canvasHeightInPx}px;`,
2018-07-05 15:27:12 +08:00
});
2019-11-18 11:44:34 +08:00
this.photoContext || (this.photoContext = wx.createCanvasContext('photo', this));
2019-11-18 11:44:34 +08:00
new Pen(this.photoContext, palette).paint(() => {
2018-07-18 18:26:58 +08:00
this.saveImgToLocal();
2018-07-05 15:27:12 +08:00
});
2019-11-15 18:20:17 +08:00
setStringPrototype(this.screenK, 1);
2018-07-10 18:24:28 +08:00
});
2018-07-05 15:27:12 +08:00
},
downloadImages(palette) {
2018-07-05 15:27:12 +08:00
return new Promise((resolve, reject) => {
let preCount = 0;
let completeCount = 0;
const paletteCopy = JSON.parse(JSON.stringify(palette));
2018-07-27 14:39:36 +08:00
if (paletteCopy.background) {
2018-07-10 18:24:28 +08:00
preCount++;
2019-11-26 11:08:07 +08:00
downloader.download(paletteCopy.background, this.properties.LRU).then((path) => {
2018-07-05 15:27:12 +08:00
paletteCopy.background = path;
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
}, () => {
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
});
}
if (paletteCopy.views) {
for (const view of paletteCopy.views) {
2018-07-27 14:39:36 +08:00
if (view && view.type === 'image' && view.url) {
2018-07-10 18:24:28 +08:00
preCount++;
/* eslint-disable no-loop-func */
2019-11-26 11:08:07 +08:00
downloader.download(view.url, this.properties.LRU).then((path) => {
2019-11-22 13:41:59 +08:00
view.originUrl = view.url;
2018-07-05 15:27:12 +08:00
view.url = path;
2018-07-27 14:39:36 +08:00
wx.getImageInfo({
src: path,
2018-07-27 14:39:36 +08:00
success: (res) => {
// 获得一下图片信息,供后续裁减使用
view.sWidth = res.width;
view.sHeight = res.height;
},
fail: (error) => {
2018-09-21 15:27:06 +08:00
// 如果图片坏了,则直接置空,防止坑爹的 canvas 画崩溃了
view.url = "";
console.error(`getImageInfo ${view.url} failed, ${JSON.stringify(error)}`);
2018-07-27 14:39:36 +08:00
},
complete: () => {
completeCount++;
if (preCount === completeCount) {
resolve(paletteCopy);
}
},
});
2018-07-05 15:27:12 +08:00
}, () => {
2018-07-10 18:24:28 +08:00
completeCount++;
2018-07-05 15:27:12 +08:00
if (preCount === completeCount) {
resolve(paletteCopy);
}
});
}
}
}
if (preCount === 0) {
resolve(paletteCopy);
}
2018-07-10 18:24:28 +08:00
});
2018-07-05 15:27:12 +08:00
},
2018-07-18 18:26:58 +08:00
saveImgToLocal() {
2018-07-05 15:27:12 +08:00
const that = this;
setTimeout(() => {
wx.canvasToTempFilePath({
canvasId: 'photo',
2019-11-21 19:41:37 +08:00
success: function (res) {
2018-07-18 18:26:58 +08:00
that.getImageInfo(res.tempFilePath);
2018-07-05 15:27:12 +08:00
},
2019-11-21 19:41:37 +08:00
fail: function (error) {
2018-07-05 15:27:12 +08:00
console.error(`canvasToTempFilePath failed, ${JSON.stringify(error)}`);
2019-11-06 17:20:44 +08:00
that.triggerEvent('imgErr', {
error: error
});
2018-07-05 15:27:12 +08:00
},
}, this);
}, 300);
},
2018-07-18 18:26:58 +08:00
getImageInfo(filePath) {
const that = this;
wx.getImageInfo({
src: filePath,
success: (infoRes) => {
if (that.paintCount > MAX_PAINT_COUNT) {
const error = `The result is always fault, even we tried ${MAX_PAINT_COUNT} times`;
console.error(error);
2019-11-06 17:20:44 +08:00
that.triggerEvent('imgErr', {
error: error
});
2018-07-18 18:26:58 +08:00
return;
}
// 比例相符时才证明绘制成功,否则进行强制重绘制
if (Math.abs((infoRes.width * that.canvasHeightInPx - that.canvasWidthInPx * infoRes.height) / (infoRes.height * that.canvasHeightInPx)) < 0.01) {
2019-11-06 17:20:44 +08:00
that.triggerEvent('imgOK', {
path: filePath
});
2018-07-18 18:26:58 +08:00
} else {
that.startPaint();
}
that.paintCount++;
},
fail: (error) => {
console.error(`getImageInfo failed, ${JSON.stringify(error)}`);
2019-11-06 17:20:44 +08:00
that.triggerEvent('imgErr', {
error: error
});
2018-07-18 18:26:58 +08:00
},
});
},
2018-07-05 15:27:12 +08:00
},
});
2019-11-06 17:20:44 +08:00
function setStringPrototype(screenK, scale) {
2018-07-10 18:24:28 +08:00
/* eslint-disable no-extend-native */
/**
* 是否支持负数
* @param {Boolean} minus 是否支持负数
2019-11-21 19:41:37 +08:00
* @param {Number} baseSize 当设置了 % 号时设置的基准值
*/
2019-11-21 19:41:37 +08:00
String.prototype.toPx = function toPx(minus, baseSize) {
2019-11-20 11:10:21 +08:00
if (this === '0') {
return 0
}
let reg;
if (minus) {
2019-11-21 19:41:37 +08:00
reg = /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px|%)$/g;
} else {
2019-11-21 19:41:37 +08:00
reg = /^[0-9]+([.]{1}[0-9]+){0,1}(rpx|px|%)$/g;
}
2018-07-10 18:24:28 +08:00
const results = reg.exec(this);
if (!this || !results) {
console.error(`The size: ${this} is illegal`);
return 0;
}
const unit = results[2];
const value = parseFloat(this);
2018-07-05 15:27:12 +08:00
2018-07-10 18:24:28 +08:00
let res = 0;
if (unit === 'rpx') {
res = Math.round(value * (screenK || 0.5) * (scale || 1));
2018-07-10 18:24:28 +08:00
} else if (unit === 'px') {
res = Math.round(value * (scale || 1));
2019-11-21 19:41:37 +08:00
} else if (unit === '%') {
res = Math.round(value * baseSize / 100);
2018-07-10 18:24:28 +08:00
}
return res;
};
2019-11-21 19:41:37 +08:00
}