This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The 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 functions.
ctx.filter = "<filter-function1> [<filter-function2] [<filter-functionN]"; ctx.filter = "none";
The filter
property accepts one or more of the following filter functions in a DOMString
.
url()
(<url>
)blur()
(<length>
)brightness()
(<percentage>
)contrast()
(<percentage>
)0%
will create a drawing that is completely black. A value of 100%
leaves the drawing unchanged.drop-shadow()
(<offset-x> <offset-y> <blur-radius> <color>
)<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>
)100%
is completely gray-scale. A value of 0%
leaves the drawing unchanged.hue-rotate()
(<angle>
)0deg
leaves the input unchanged.invert()
(<percentage>
)100%
means complete inversion. A value of 0%
leaves the drawing unchanged.opacity()
(<percentage>
)0%
means completely transparent. A value of 100%
leaves the drawing unchanged.saturate()
(<percentage>
)0%
means completely un-saturated. A value of 100%
leaves the drawing unchanged.sepia()
(<percentage>
)0%
leaves the drawing unchanged.none
filter
propertyThis is just a simple code snippet using the filter
property.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.filter = 'blur(4px)'; ctx.font = '48px serif'; ctx.fillText('Hello world', 50, 100);
Edit the code below and see your changes update live in the canvas (and make sure to use a browser which supports this feature, see the compatibility table):
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.filter' in that specification. | Living Standard | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 52 | ? | 49
|
No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 52 | 52 | ? | 49
|
No | No | 6.0 |
CanvasRenderingContext2D
filter
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter