151 lines
6.5 KiB
JavaScript
151 lines
6.5 KiB
JavaScript
var t = require("../@babel/runtime/helpers/interopRequireDefault"), e = require("../@babel/runtime/helpers/createForOfIteratorHelper"), n = t(require("../DD5CBCF0E645A4CFBB3AD4F73DA18D45.js")), i = t(require("../CB7FD377E645A4CFAD19BB7000A18D45.js")), r = require("../4110C867E645A4CF2776A060E1D18D45.js"), a = new i.default();
|
|
|
|
Component({
|
|
canvasWidthInPx: 0,
|
|
canvasHeightInPx: 0,
|
|
paintCount: 0,
|
|
properties: {
|
|
customStyle: {
|
|
type: String
|
|
},
|
|
palette: {
|
|
type: Object,
|
|
observer: function(t, e) {
|
|
this.isNeedRefresh(t, e) && (this.paintCount = 0, this.startPaint());
|
|
}
|
|
},
|
|
dirty: {
|
|
type: Boolean,
|
|
value: !1
|
|
}
|
|
},
|
|
data: {
|
|
picURL: "",
|
|
showCanvas: !0,
|
|
painterStyle: ""
|
|
},
|
|
attached: function() {
|
|
String.prototype.toPx = function(t) {
|
|
var e = (t ? /^-?[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g : /^[0-9]+([.]{1}[0-9]+){0,1}(rpx|px)$/g).exec(this);
|
|
if (!this || !e) return console.log("The size: ".concat(this, " is illegal")), 0;
|
|
var n = e[2], i = parseFloat(this), r = 0;
|
|
return "rpx" === n ? r = Math.round(i * o) : "px" === n && (r = i), r;
|
|
};
|
|
},
|
|
methods: {
|
|
isEmpty: function(t) {
|
|
for (var e in t) return !1;
|
|
return !0;
|
|
},
|
|
isNeedRefresh: function(t, e) {
|
|
return !(!t || this.isEmpty(t) || this.data.dirty && r.equal(t, e));
|
|
},
|
|
startPaint: function() {
|
|
var t = this;
|
|
if (!this.isEmpty(this.properties.palette)) {
|
|
if (!getApp().systemInfo || !getApp().systemInfo.screenWidth) try {
|
|
getApp().systemInfo = wx.getSystemInfoSync();
|
|
} catch (t) {
|
|
t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
|
|
var e = "Painter get system info failed, ".concat(JSON.stringify(t));
|
|
return that.triggerEvent("imgErr", {
|
|
error: e
|
|
}), void console.log(e);
|
|
}
|
|
o = getApp().systemInfo.screenWidth / 750, this.downloadImages().then(function(e) {
|
|
var i = e.width, r = e.height;
|
|
if (t.canvasWidthInPx = i.toPx(), t.canvasHeightInPx = r.toPx(), i && r) {
|
|
t.setData({
|
|
painterStyle: "width:".concat(i, ";height:").concat(r, ";")
|
|
});
|
|
var a = wx.createCanvasContext("k-canvas", t);
|
|
new n.default(a, e).paint(function() {
|
|
t.saveImgToLocal();
|
|
});
|
|
} else console.log("You should set width and height correctly for painter, width: ".concat(i, ", height: ").concat(r));
|
|
});
|
|
}
|
|
},
|
|
downloadImages: function() {
|
|
var t = this;
|
|
return new Promise(function(n, i) {
|
|
var r = 0, o = 0, s = JSON.parse(JSON.stringify(t.properties.palette));
|
|
s.background && (r++, a.download(s.background).then(function(t) {
|
|
s.background = t, o++, r === o && n(s);
|
|
}, function() {
|
|
o++, r === o && n(s);
|
|
})), s.views && function() {
|
|
var i, c = t, g = e(s.views);
|
|
try {
|
|
var h = function() {
|
|
var t = i.value;
|
|
t && "image" === t.type && t.url && (r++, a.download(t.url).then(function(e) {
|
|
t.url = e, wx.getImageInfo({
|
|
src: t.url,
|
|
success: function(e) {
|
|
t.sWidth = e.width, t.sHeight = e.height;
|
|
},
|
|
fail: function(t) {
|
|
console.log("imgDownloadErr failed, ".concat(JSON.stringify(t))), c.triggerEvent("imgDownloadErr", {
|
|
error: t
|
|
});
|
|
},
|
|
complete: function() {
|
|
o++, r === o && n(s);
|
|
}
|
|
});
|
|
}, function() {
|
|
o++, r === o && n(s);
|
|
}));
|
|
};
|
|
for (g.s(); !(i = g.n()).done; ) h();
|
|
} catch (t) {
|
|
t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t), g.e(t);
|
|
} finally {
|
|
g.f();
|
|
}
|
|
}(), 0 === r && n(s);
|
|
});
|
|
},
|
|
saveImgToLocal: function() {
|
|
var t = this, e = this;
|
|
setTimeout(function() {
|
|
wx.canvasToTempFilePath({
|
|
canvasId: "k-canvas",
|
|
success: function(t) {
|
|
e.getImageInfo(t.tempFilePath);
|
|
},
|
|
fail: function(t) {
|
|
console.log("canvasToTempFilePath failed, ".concat(JSON.stringify(t))), e.triggerEvent("imgErr", {
|
|
error: t
|
|
});
|
|
}
|
|
}, t);
|
|
}, 300);
|
|
},
|
|
getImageInfo: function(t) {
|
|
var e = this;
|
|
wx.getImageInfo({
|
|
src: t,
|
|
success: function(n) {
|
|
if (e.paintCount > 5) {
|
|
var i = "The result is always fault, even we tried ".concat(5, " times");
|
|
return console.log(i), void e.triggerEvent("imgErr", {
|
|
error: i
|
|
});
|
|
}
|
|
Math.abs((n.width * e.canvasHeightInPx - e.canvasWidthInPx * n.height) / (n.height * e.canvasHeightInPx)) < .01 ? e.triggerEvent("imgOK", {
|
|
path: t
|
|
}) : e.startPaint(), e.paintCount++;
|
|
},
|
|
fail: function(t) {
|
|
console.log("getImageInfo failed, ".concat(JSON.stringify(t))), e.triggerEvent("imgErr", {
|
|
error: t
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
var o = .5; |