这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
CanvasRenderingContext2D
.filter
property of the Canvas 2D API provides filter effects such as blurring and grayscaling. It is similar to the CSS
filter
property and accepts the same values.
ctx.filter = "<filter-function1> [<filter-function2>] [<filter-functionN>]"; ctx.filter = "none";
filter
property accepts a value of
"none"
or one or more of the following filter functions in a
DOMString
.
url()
<url>
. Takes an IRI pointing to an SVG filter element, which may be embedded in an external XML file.
blur()
<length>
. Applies a Gaussian blur to the drawing. It defines the value of the standard deviation to the Gaussian function, i.e., how many pixels on the screen blend into each other; thus, a larger value will create more blur. A value of
0
leaves the input unchanged.
brightness()
<percentage>
. Applies a linear multiplier to the drawing, making it appear brighter or darker. A value under
100%
darkens the image, while a value over
100%
brightens it. A value of
0%
will create an image that is completely black, while a value of
100%
leaves the input unchanged.
contrast()
<percentage>
. Adjusts the contrast of the drawing. A value of
0%
will create a drawing that is completely black. A value of
100%
leaves the drawing unchanged.
drop-shadow()
<offset-x>
: See
<length>
for possible units. Specifies the horizontal distance of the shadow.
<offset-y>
: See
<length>
for possible units. Specifies the vertical distance of the shadow.
<blur-radius>
: The larger this value, the bigger the blur, so the shadow becomes bigger and lighter. Negative values are not allowed.
<color>
: See
<color>
values for possible keywords and notations.
grayscale()
<percentage>
. Converts the drawing to grayscale. A value of
100%
is completely grayscale. A value of
0%
leaves the drawing unchanged.
hue-rotate()
<angle>
. Applies a hue rotation on the drawing. A value of
0deg
leaves the input unchanged.
invert()
<percentage>
. Inverts the drawing. A value of
100%
means complete inversion. A value of
0%
leaves the drawing unchanged.
opacity()
<percentage>
. Applies transparency to the drawing. A value of
0%
means completely transparent. A value of
100%
leaves the drawing unchanged.
saturate()
<percentage>
. Saturates the drawing. A value of
0%
means completely un-saturated. A value of
100%
leaves the drawing unchanged.
sepia()
<percentage>
. Converts the drawing to sepia. A value of
100%
means completely sepia. A value of
0%
leaves the drawing unchanged.
none
No filter is applied. Initial value.
To view these examples, make sure to use a browser that supports this feature; see the compatibility table below.
This example blurs a piece of text using the
filter
特性。
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.filter = 'blur(4px)';
ctx.font = '48px serif';
ctx.fillText('Hello world', 50, 100);
You can combine as many filters as you like. This example applies the
contrast
,
sepia
,和
drop-shadow
filters to a photo of a gecko.
<canvas id="canvas"></canvas>
<div style="display:none;">
<img id="source"
src="https://interactive-examples.mdn.mozilla.net/media/examples/gecko-320-213.jpg">
</div>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const image = document.getElementById('source');
image.addEventListener('load', e => {
ctx.filter = 'contrast(1.4) sepia(1) drop-shadow(9px 9px 2px #e81)';
ctx.drawImage(image, 10, 10, 180, 120);
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.filter' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
filter
|
Chrome 52 | Edge 79 |
Firefox
49
|
IE No | Opera No | Safari No | WebView Android 52 | Chrome Android 52 |
Firefox Android
49
|
Opera Android No | Safari iOS No | Samsung Internet Android 6.0 |
完整支持
不支持
实验。期望将来行为有所改变。
用户必须明确启用此特征。
CanvasRenderingContext2D
filter
<filter-function>
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()