const ctx = wx.createCanvasContext('photo_canvas'); const SIZE = 200 const ratio = Math.max(res.width / SIZE, res.height / SIZE, 1) const smallWidth = Math.trunc(res.width / ratio) const smallHeight = Math.trunc(res.height / ratio) const x = (smallWidth - res.width) / 2; const y = (smallHeight - res.height) / 2; // 将宽高大小设置到页面的canvas元素(checkCanvas)上 ctx.drawImage(tempFilePaths[index], x, y, res.width, res.height, 0, 0, smallWidth, smallHeight) ctx.draw(false, function () { wx.canvasToTempFilePath({ width: smallWidth, height: smallHeight, destWidth: smallWidth, destHeight: smallHeight, canvasId: 'photo_canvas',这段代码输出的图片会偏移,无法居中显示,请问是什么原因?
时间: 2024-01-02 12:05:22 浏览: 31
这段代码输出的图片偏移的原因可能是因为在调用`ctx.drawImage`方法时,指定的图像位置和大小不正确。具体来说,如果要将图像居中显示,应该将其绘制在画布的中心,而不是从左上角开始绘制。你可以尝试修改代码如下:
```
const centerX = smallWidth / 2;
const centerY = smallHeight / 2;
ctx.drawImage(tempFilePaths[index], 0, 0, res.width, res.height, centerX - res.width / 2, centerY - res.height / 2, res.width, res.height);
ctx.draw(false, function () {
wx.canvasToTempFilePath({
width: smallWidth,
height: smallHeight,
destWidth: smallWidth,
destHeight: smallHeight,
canvasId: 'photo_canvas',
success: function (res) {
console.log(res.tempFilePath);
}
});
});
```
这里首先计算出画布的中心坐标,然后将图像绘制在中心位置,使其居中显示。
相关问题
const ctx = wx.createCanvasContext('photo_canvas'); const SIZE = 200 const ratio = Math.max(res.width / SIZE, res.height / SIZE, 1) const smallWidth = Math.trunc(res.width / ratio) const smallHeight = Math.trunc(res.height / ratio) // 将宽高大小设置到页面的canvas元素(checkCanvas)上 ctx.drawImage(tempFilePaths[index], 0, 0, smallWidth, smallHeight) ctx.draw(false, function () { wx.canvasToTempFilePath({ width: smallWidth, height: smallHeight, destWidth: smallWidth, destHeight: smallHeight, canvasId: 'photo_canvas',请问怎么修改代码中的参数,让输出图片保持原始比列,而不是剪切图片或形变的图片
要让输出的图片保持原始比例,你可以修改代码如下:
```
const ctx = wx.createCanvasContext('photo_canvas');
const SIZE = 200;
const ratio = Math.max(res.width / SIZE, res.height / SIZE, 1);
const smallWidth = Math.trunc(res.width / ratio);
const smallHeight = Math.trunc(res.height / ratio);
const x = (smallWidth - res.width) / 2;
const y = (smallHeight - res.height) / 2;
ctx.drawImage(tempFilePaths[index], x, y, res.width, res.height, 0, 0, smallWidth, smallHeight);
ctx.draw(false, function () {
wx.canvasToTempFilePath({
width: smallWidth,
height: smallHeight,
destWidth: smallWidth,
destHeight: smallHeight,
canvasId: 'photo_canvas',
success: function (res) {
console.log(res.tempFilePath);
}
});
});
```
这里使用了`drawImage`方法的另一个重载版本,它可以指定在画布上绘制图像的位置和大小,以及将图像的哪一部分裁剪出来绘制。首先计算出在画布上绘制图像的起始坐标,使其居中显示,然后指定绘制的图像大小为原始大小,最后指定输出的图片大小为缩小后的大小。
wx.getImageInfo({ src: tempFilePaths, success: function (res) { const SIZE = 300 const ratio = Math.max((res.width/SIZE)*1, (res.height/SIZE)*1, 1) const smallWidth = Math.trunc(res.width / ratio) const smallHeight = Math.trunc(res.height / ratio) console.log('宽度:',smallWidth) console.log('高度:',smallHeight) console.log('缩放比例:',ratio) const ctx = wx.createCanvasContext('photo_canvas'); ctx.drawImage(tempFilePaths, 0, 0, smallWidth, smallHeight); ctx.draw(false, function () { wx.canvasToTempFilePath({ width: smallWidth, height: smallHeight, destWidth: smallWidth, destHeight: smallHeight, canvasId: 'photo_canvas', success: function (res) { var filePaths = res.tempFilePath wx.getFileSystemManager().unlink({ filePath: res.tempFilePath, success: function(res) { console.log('删除临时图片成功') // 清空画布 const ctx = wx.createCanvasContext('photo_canvas'); ctx.clearRect(0, 0, canvasWidth, canvasHeight) }, fail: function(res) { console.log('删除临时图片失败', res) } })这段代码中删除临时图片是失败的为什么?
根据你提供的代码,可能是因为在删除临时图片之前,清空画布的代码有误。在清空画布时,你使用了未定义的 `canvasWidth` 和 `canvasHeight` 变量。你需要先定义这两个变量,或者直接使用 `smallWidth` 和 `smallHeight` 变量来清空画布。
修改后的代码如下:
```
wx.getImageInfo({
src: tempFilePaths,
success: function (res) {
const SIZE = 300
const ratio = Math.max((res.width/SIZE)*1, (res.height/SIZE)*1, 1)
const smallWidth = Math.trunc(res.width / ratio)
const smallHeight = Math.trunc(res.height / ratio)
console.log('宽度:',smallWidth)
console.log('高度:',smallHeight)
console.log('缩放比例:',ratio)
const ctx = wx.createCanvasContext('photo_canvas');
ctx.drawImage(tempFilePaths, 0, 0, smallWidth, smallHeight);
ctx.draw(false, function () {
wx.canvasToTempFilePath({
width: smallWidth,
height: smallHeight,
destWidth: smallWidth,
destHeight: smallHeight,
canvasId: 'photo_canvas',
success: function (res) {
var filePaths = res.tempFilePath
wx.getFileSystemManager().unlink({
filePath: res.tempFilePath,
success: function(res) {
console.log('删除临时图片成功')
// 清空画布
const ctx = wx.createCanvasContext('photo_canvas');
ctx.clearRect(0, 0, smallWidth, smallHeight)
},
fail: function(res) {
console.log('删除临时图片失败', res)
}
})
}
})
})
}
})
```
请注意,如果删除临时图片的问题还未解决,你可以在控制台输出 `res` 对象来查看具体的错误信息,以便更好地定位问题。