javascript - chrome 77 更新后图像显示不正确
问题描述
在我们的网站中,我们可以使用 dropzone 一起上传多张图片。为 android 更新 chrome 后,压缩后的大尺寸图像显示为黑色图像。但是我们在 ios 或桌面 chrome 中没有这个问题,问题只在 android chrome 中。
我怀疑这是由本机延迟加载引起的。有没有办法禁用它?
解决方案
最后在检查了所有压缩算法后,我发现了 Android Chrome 77 中的问题。
在我们的算法中,从文件加载图像后,我们使用 canvas.getContext('2d').drawImage 将图像绘制到画布中。然后为了调整图片的大小,我们在画布上使用 drawImage() 来拥有新的画布并听到 chrome 是否破坏了算法。简短的答案是 canvas.getContext('2d').drawImage 函数可以正确使用图像作为输入,但不能使用画布作为输入。
为了解决这个问题,我们首先计算正确的宽度和高度,然后读取具有所需尺寸的图像。
作品:
var canvas = document.createElement('canvas');
canvas.width = img.width/2;
canvas.height = img.height/2;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
并且不起作用:
var canvas = document.createElement('canvas');
canvas.getContext('2d').drawImage(img, 0, 0);
var newCanvas = document.createElement('canvas');
newCanvas.width = canvas.width / 2;
newCanvas.height = canvas.height / 2;
newCanvas.getContext('2d').drawImage(canvas, 0, 0, canvas.width/2, canvas.height/2);
归根结底,Chrome 77 无法从画布绘制画布。
推荐阅读
- c# - 如何从我的 C# 应用程序中检查新 Microsoft Edge Chromium 的版本?
- react-native - 移除推送通知监听器 React Native Expo
- php - PHP cURL 使用我的 IP 地址而不是服务器?
- python - 使用新数据测试文本分类 ML 模型失败
- javascript - 将对象数组转换为嵌套对象数组js
- angular - Angular Link 未通向组件
- r - 为什么我的新 var 名称显示为“... <- NULL”
- selenium - 使用 selenium webdriver 单击隐藏元素
- java - Spring Boot - SimpMessagingTemplate 没有在具有 Spring Boot 安全性的 Spring Boot WebSocket 中向客户端发送消息
- java - 媒体编解码器完全耗尽输出缓冲区