Merge pull request #10 from Kujiale-Mobile/canvas2d

Canvas2d
This commit is contained in:
0JARVIS0 2020-12-01 10:37:57 +08:00 committed by GitHub
commit a6ff470108
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 32 deletions

View File

@ -46,7 +46,7 @@ Component({
dancePalette: { dancePalette: {
type: Object, type: Object,
observer: function (newVal, oldVal) { observer: function (newVal, oldVal) {
if (!this.isEmpty(newVal)) { if (!this.isEmpty(newVal) && !this.properties.use2D) {
this.initDancePalette(newVal); this.initDancePalette(newVal);
} }
}, },
@ -72,7 +72,7 @@ Component({
action: { action: {
type: Object, type: Object,
observer: function (newVal, oldVal) { observer: function (newVal, oldVal) {
if (newVal && !this.isEmpty(newVal)) { if (newVal && !this.isEmpty(newVal) && !this.properties.use2D) {
this.doAction(newVal, (callbackInfo) => { this.doAction(newVal, (callbackInfo) => {
this.movingCache = callbackInfo this.movingCache = callbackInfo
}, false, true) }, false, true)
@ -232,6 +232,9 @@ Component({
}, },
doAction(action, callback, isMoving, overwrite) { doAction(action, callback, isMoving, overwrite) {
if (this.properties.use2D) {
return;
}
let newVal = null let newVal = null
if (action) { if (action) {
newVal = action.view newVal = action.view
@ -622,6 +625,9 @@ Component({
}, },
initDancePalette() { initDancePalette() {
if (this.properties.use2D) {
return;
}
this.isDisabled = true; this.isDisabled = true;
this.initScreenK(); this.initScreenK();
this.downloadImages(this.properties.dancePalette).then(async (palette) => { this.downloadImages(this.properties.dancePalette).then(async (palette) => {
@ -656,35 +662,49 @@ Component({
startPaint() { startPaint() {
this.initScreenK(); this.initScreenK();
const {
width,
height
} = this.properties.palette;
this.downloadImages(this.properties.palette).then(async (palette) => { if (!width || !height) {
const { console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`);
width, return;
height }
} = palette;
if (!width || !height) { let needScale = false;
console.error(`You should set width and height correctly for painter, width: ${width}, height: ${height}`); // 生成图片时,根据设置的像素值重新绘制
return; if (width.toPx() !== this.canvasWidthInPx) {
}
// 生成图片时,根据设置的像素值重新绘制
this.canvasWidthInPx = width.toPx(); this.canvasWidthInPx = width.toPx();
if (this.properties.widthPixels) { needScale = this.properties.use2D;
setStringPrototype(this.screenK, this.properties.widthPixels / this.canvasWidthInPx) }
this.canvasWidthInPx = this.properties.widthPixels if (this.properties.widthPixels) {
} setStringPrototype(this.screenK, this.properties.widthPixels / this.canvasWidthInPx)
this.canvasWidthInPx = this.properties.widthPixels
}
if (this.canvasHeightInPx !== height.toPx()) {
this.canvasHeightInPx = height.toPx(); this.canvasHeightInPx = height.toPx();
this.setData({ needScale = needScale || this.properties.use2D;
photoStyle: `width:${this.canvasWidthInPx}px;height:${this.canvasHeightInPx}px;`, }
this.setData({
photoStyle: `width:${this.canvasWidthInPx}px;height:${this.canvasHeightInPx}px;`,
}, function () {
this.downloadImages(this.properties.palette).then(async palette => {
if (!this.photoContext) {
this.photoContext = await this.getCanvasContext(this.properties.use2D, 'photo');
}
if (needScale) {
const scale = getApp().systemInfo.pixelRatio;
this.photoContext.width = this.canvasWidthInPx * scale;
this.photoContext.height = this.canvasHeightInPx * scale;
this.photoContext.scale(scale, scale);
}
new Pen(this.photoContext, palette).paint(() => {
this.saveImgToLocal();
});
setStringPrototype(this.screenK, this.properties.scaleRatio);
}); });
this.photoContext || (this.photoContext = await this.getCanvasContext(this.properties.use2D, 'photo'));
new Pen(this.photoContext, palette).paint(() => {
this.saveImgToLocal();
});
setStringPrototype(this.screenK, this.properties.scaleRatio);
}); });
}, },
@ -784,10 +804,6 @@ Component({
that.canvasNode = res[0].node; that.canvasNode = res[0].node;
const ctx = that.canvasNode.getContext('2d'); const ctx = that.canvasNode.getContext('2d');
const wxCanvas = new WxCanvas('2d', ctx, id, true, that.canvasNode); const wxCanvas = new WxCanvas('2d', ctx, id, true, that.canvasNode);
const dpr = Math.min(2, getApp().systemInfo.pixelRatio)
wxCanvas.width = res[0].width * dpr
wxCanvas.height = res[0].height * dpr
wxCanvas.scale(dpr, dpr)
resolve(wxCanvas); resolve(wxCanvas);
}); });
} else { } else {

View File

@ -14,8 +14,8 @@
disable-scroll="{{true}}" /> disable-scroll="{{true}}" />
</block> </block>
<block wx:if="{{use2D}}"> <block wx:if="{{use2D}}">
<canvas type="2d" id="photo" style="{{photoStyle}};position: absolute; left: -9999px; top: -9999rpx;" /> <canvas type="2d" id="photo" style="{{photoStyle}};" />
<canvas type="2d" id="bottom" style="{{painterStyle}};position: absolute;" /> <!-- <canvas type="2d" id="bottom" style="{{painterStyle}};position: absolute;" />
<canvas type="2d" id="k-canvas" style="{{painterStyle}};position: absolute;" /> <canvas type="2d" id="k-canvas" style="{{painterStyle}};position: absolute;" />
<canvas type="2d" id="top" style="{{painterStyle}};position: absolute;" /> <canvas type="2d" id="top" style="{{painterStyle}};position: absolute;" />
<canvas <canvas
@ -26,6 +26,6 @@
bindtouchmove="onTouchMove" bindtouchmove="onTouchMove"
bindtouchend="onTouchEnd" bindtouchend="onTouchEnd"
bindtouchcancel="onTouchCancel" bindtouchcancel="onTouchCancel"
disable-scroll="{{true}}" /> disable-scroll="{{true}}" /> -->
</block> </block>
</view> </view>