CanvasRenderingContext2D.imageSmoothingEnabled - Web APIs 编辑
The imageSmoothingEnabled
property of the CanvasRenderingContext2D
interface, part of the Canvas API, determines whether scaled images are smoothed (true
, default) or not (false
). On getting the imageSmoothingEnabled
property, the last value it was set to is returned.
This property is useful for games and other apps that use pixel art. When enlarging images, the default resizing algorithm will blur the pixels. Set this property to false
to retain the pixels' sharpness.
Note: You can adjust the smoothing quality with the imageSmoothingQuality
property.
Syntax
ctx.imageSmoothingEnabled = value;
Options
value
- A
Boolean
indicating whether to smooth scaled images or not. The default value istrue
.
Examples
Disabling image smoothing
This example compares three images. The first image is drawn at its natural size, the second is scaled to 3X and drawn with image smoothing enabled, and the third is scaled to 3X but drawn with image smoothing disabled.
HTML
<canvas id="canvas" width="460" height="210"></canvas>
JavaScript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.font = '16px sans-serif';
ctx.textAlign = 'center';
const img = new Image();
img.src = 'https://www.wenjiangs.com/wp-content/uploads/2020/mozilla/star.png';
img.onload = function() {
const w = img.width,
h = img.height;
ctx.fillText('Source', w * .5, 20);
ctx.drawImage(img, 0, 24, w, h);
ctx.fillText('Smoothing = TRUE', w * 2.5, 20);
ctx.imageSmoothingEnabled = true;
ctx.drawImage(img, w, 24, w * 3, h * 3);
ctx.fillText('Smoothing = FALSE', w * 5.5, 20);
ctx.imageSmoothingEnabled = false;
ctx.drawImage(img, w * 4, 24, w * 3, h * 3);
};
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.imageSmoothingEnabled' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
See also
- The interface defining this property:
CanvasRenderingContext2D
CanvasRenderingContext2D.imageSmoothingQuality
image-rendering
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论