Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

迁移 Canvas 2D 接口,修复安卓渲染回调过早 #80

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/weapp.qrcode.common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/weapp.qrcode.esm.js

Large diffs are not rendered by default.

43 changes: 34 additions & 9 deletions dist/weapp.qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ function utf16to8(str) {
function drawQrcode(options) {
options = options || {};
options = extend(true, {
canvas: null,
width: 256,
height: 256,
x: 0,
Expand All @@ -1230,8 +1231,8 @@ function drawQrcode(options) {
}
}, options);

if (!options.canvasId && !options.ctx) {
console.warn('please set canvasId or ctx!');
if (!options.ctx) {
console.warn('please set ctx!');
return;
}

Expand All @@ -1248,7 +1249,12 @@ function drawQrcode(options) {
if (options.ctx) {
ctx = options.ctx;
} else {
ctx = options._this ? wx.createCanvasContext && wx.createCanvasContext(options.canvasId, options._this) : wx.createCanvasContext && wx.createCanvasContext(options.canvasId);
console.error('please set ctx!');
return;
}
if (!options.canvas) {
console.error('please set canvas!');
return;
}

// compute tileW/tileH based on options.width/options.height
Expand All @@ -1259,20 +1265,39 @@ function drawQrcode(options) {
for (var row = 0; row < qrcode.getModuleCount(); row++) {
for (var col = 0; col < qrcode.getModuleCount(); col++) {
var style = qrcode.isDark(row, col) ? options.foreground : options.background;
ctx.setFillStyle(style);
// From WeChat MiniProgram base library 1.9.90 and later, maintenance has been discontinued for this API
// https://developers.weixin.qq.com/miniprogram/en/dev/api/canvas/CanvasContext.setFillStyle.html
if (ctx.setFillStyle) {
ctx.setFillStyle(style);
} else {
// Start from base library version 1.9.90.
// https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.html
ctx.fillStyle = style;
}

var w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW);
var h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW);
ctx.fillRect(Math.round(col * tileW) + options.x, Math.round(row * tileH) + options.y, w, h);
}
}

if (options.image.imageResource) {
ctx.drawImage(options.image.imageResource, options.image.dx, options.image.dy, options.image.dWidth, options.image.dHeight);
const image = options.canvas.createImage();
image.onload = () => {
ctx.drawImage(image, options.image.dx, options.image.dy, options.image.dWidth, options.image.dHeight);

var callbackHandle = function (e) {
options.callback && options.callback(e);
};
// RenderingContext without draw function
if (ctx.draw) {
ctx.draw(false, callbackHandle);
} else {
callbackHandle();
}
};
image.src = options.image.imageResource;
}

ctx.draw(false, function (e) {
options.callback && options.callback(e);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion dist/weapp.qrcode.min.js

Large diffs are not rendered by default.

43 changes: 34 additions & 9 deletions examples/wechat-app/utils/weapp.qrcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,7 @@ function utf16to8(str) {
function drawQrcode(options) {
options = options || {};
options = extend(true, {
canvas: null,
width: 256,
height: 256,
x: 0,
Expand All @@ -1230,8 +1231,8 @@ function drawQrcode(options) {
}
}, options);

if (!options.canvasId && !options.ctx) {
console.warn('please set canvasId or ctx!');
if (!options.ctx) {
console.warn('please set ctx!');
return;
}

Expand All @@ -1248,7 +1249,12 @@ function drawQrcode(options) {
if (options.ctx) {
ctx = options.ctx;
} else {
ctx = options._this ? wx.createCanvasContext && wx.createCanvasContext(options.canvasId, options._this) : wx.createCanvasContext && wx.createCanvasContext(options.canvasId);
console.error('please set ctx!');
return;
}
if (!options.canvas) {
console.error('please set canvas!');
return;
}

// compute tileW/tileH based on options.width/options.height
Expand All @@ -1259,20 +1265,39 @@ function drawQrcode(options) {
for (var row = 0; row < qrcode.getModuleCount(); row++) {
for (var col = 0; col < qrcode.getModuleCount(); col++) {
var style = qrcode.isDark(row, col) ? options.foreground : options.background;
ctx.setFillStyle(style);
// From WeChat MiniProgram base library 1.9.90 and later, maintenance has been discontinued for this API
// https://developers.weixin.qq.com/miniprogram/en/dev/api/canvas/CanvasContext.setFillStyle.html
if (ctx.setFillStyle) {
ctx.setFillStyle(style);
} else {
// Start from base library version 1.9.90.
// https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.html
ctx.fillStyle = style;
}

var w = Math.ceil((col + 1) * tileW) - Math.floor(col * tileW);
var h = Math.ceil((row + 1) * tileW) - Math.floor(row * tileW);
ctx.fillRect(Math.round(col * tileW) + options.x, Math.round(row * tileH) + options.y, w, h);
}
}

if (options.image.imageResource) {
ctx.drawImage(options.image.imageResource, options.image.dx, options.image.dy, options.image.dWidth, options.image.dHeight);
const image = options.canvas.createImage();
image.onload = () => {
ctx.drawImage(image, options.image.dx, options.image.dy, options.image.dWidth, options.image.dHeight);

var callbackHandle = function (e) {
options.callback && options.callback(e);
};
// RenderingContext without draw function
if (ctx.draw) {
ctx.draw(false, callbackHandle);
} else {
callbackHandle();
}
};
image.src = options.image.imageResource;
}

ctx.draw(false, function (e) {
options.callback && options.callback(e);
});
}
}

Expand Down
Loading