CanvasRenderingContext2D: filter property
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
{{APIRef("Canvas API")}}
The **CanvasRenderingContext2D.filter** property of the Canvas 2D API provides filter effects such as blurring and grayscaling. It is similar to the CSS {{cssxref("filter")}} property and accepts the same values.
Value
The filter property accepts a value of "none" or one or more of the following filter functions in a string.
- {{cssxref("url_function", "url()")}}
- : A CSS {{cssxref("url_function", "url()")}}. Takes any URL that resolves to SVG filter element.
This can be the ID of an element, a path to external XML file, or even a data encoded SVG value.
- {{cssxref("filter-function/blur", "blur()")}}
- : A CSS {{cssxref("<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.
- {{cssxref("filter-function/brightness", "brightness()")}}
- : A CSS {{cssxref("<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.
- {{cssxref("filter-function/contrast", "contrast()")}}
- : A CSS {{cssxref("<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.
- {{cssxref("filter-function/drop-shadow", "drop-shadow()")}}
- : Applies a drop shadow effect to the drawing. A drop shadow is effectively a blurred,
offset version of the drawing's alpha mask drawn in a particular color, composited below the drawing. This function takes up to five arguments:
<offset-x>- : See {{cssxref("<length>")}} for possible
units. Specifies the horizontal distance of the shadow.
<offset-y>- : See {{cssxref("<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 {{cssxref("<color>")}} values for possible
keywords and notations.
- {{cssxref("filter-function/grayscale", "grayscale()")}}
- : A CSS {{cssxref("<percentage>")}}. Converts the drawing to grayscale. A value
of 100% is completely grayscale. A value of 0% leaves the drawing unchanged.
- {{cssxref("filter-function/hue-rotate", "hue-rotate()")}}
- : A CSS {{cssxref("<angle>")}}. Applies a hue rotation on the drawing. A value
of 0deg leaves the input unchanged.
- {{cssxref("filter-function/invert", "invert()")}}
- : A CSS {{cssxref("<percentage>")}}. Inverts the drawing. A value of
100% means complete inversion. A value of 0% leaves the drawing unchanged.
- {{cssxref("filter-function/opacity", "opacity()")}}
- : A CSS {{cssxref("<percentage>")}}. Applies transparency to the drawing. A
value of 0% means completely transparent. A value of 100% leaves the drawing unchanged.
- {{cssxref("filter-function/saturate", "saturate()")}}
- : A CSS {{cssxref("<percentage>")}}. Saturates the drawing. A value of
0% means completely un-saturated. A value of 100% leaves the drawing unchanged.
- {{cssxref("filter-function/sepia", "sepia()")}}
- : A CSS {{cssxref("<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.
Examples
To view these examples, make sure to use a browser that supports this feature; see the compatibility table below.
Applying a blur
This example blurs a piece of text using the filter property.
HTML
<canvas id="canvas"></canvas>JavaScript
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.filter = "blur(4px)";
ctx.font = "48px serif";
ctx.fillText("Hello world", 50, 100);Result
{{ EmbedLiveSample('Applying_a_blur', 700, 180) }}
Applying multiple filters
You can combine as many filters as you like. This example applies the contrast, sepia, and drop-shadow filters to a photo of a rhino.
HTML
<canvas id="canvas" width="400" height="150"></canvas>
<div class="hidden">
<img
id="source"
src="https://mdn.github.io/shared-assets/images/examples/rhino.jpg" />
</div>JavaScript
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const image = document.getElementById("source");
image.addEventListener("load", (e) => {
// Draw unfiltered image
ctx.drawImage(image, 0, 0, image.width * 0.6, image.height * 0.6);
// Draw image with filter
ctx.filter = "contrast(1.4) sepia(1) drop-shadow(-9px 9px 3px #ee8811)";
ctx.drawImage(image, 400, 0, -image.width * 0.6, image.height * 0.6);
});Result
{{ EmbedLiveSample('Applying_multiple_filters', 700, 180) }}
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- The interface defining this property: {{domxref("CanvasRenderingContext2D")}}
- CSS {{cssxref("filter")}}
- CSS {{cssxref("<filter-function>")}}