This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The CanvasRenderingContext2D
.filter
property of the Canvas 2D API provides filter effects like blurring or gray-scaling. 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>)
url()
function takes the location of an XML file that specifies an SVG filter, and may include an anchor to a specific filter element.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(<degree>)
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(5px)'; ctx.font = '48px serif'; ctx.strokeText('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 |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.filter' in that specification. | Living Standard | Initial definition. |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 52.0 | 49 (49) [1] | No support | No support | No support |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | No support | No support | 49.0 (49) [1] | No support | No support | No support |
[1] The filter
property was deactivated by default in versions 35-48. Set the flag canvas.filters.enabled
to true
in these versions.
Starting with Gecko 41 (Firefox 41 / Thunderbird 41 / SeaMonkey 2.38), the initial value is none
, see (bug 1163124).
CanvasRenderingContext2D
filter
© 2005–2017 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