2D 上下文filter 属性
您可以通过 2D 上下文过滤器属性将一些标准过滤器应用于画布。
ctx.filter = "blur(5px)"
过滤器应用于新呈现的内容,而不是现有内容。
ctx.fillRect(10,10,20,10); // not blurred
ctx.filter = "blur (5px)";
ctx.fillRect(10,30,20,10); //blurred but first rect remains unblurred
有一些内置过滤器。这些单位就像 CSS 一样,必须后固定。长度“px”,百分比“%”,度数“deg”
您还可以通过 url 使用 SVG 过滤器。我从未尝试过使用该方法,但假设我将涵盖所有可用的 SVG 过滤器。
当我第一次使用过滤器属性时,它非常慢。但现在我在动画中使用它们,因为性能越来越好。但请务必小心,因为它们尚未达到 CSS 和 SVG 的性能,通常是因为渲染管理技术。
画布过滤方法列表。
"url(url)" // url points to XML file containing SVG filter,
// can be to anchor in SVG to filter element.
"blur(length)"
"brightness(percentage)"
"contrast(percentage)"
"drop-shadow(offsetX, offsetY, blurRadius, color)"
"grayscale(percentage)"
'hue-rotate(degree)"
"invert(percentage)"
"opacity(percentage)"
"saturate(percentage)"
"sepia(percentage)"
*注意; filter 是实验性的,见 MDN 2D context filter
重新 SVG 过滤器url() 功能。来自 MDN 2017 年 9 月 15 日
- url()。 url() 函数获取指定 SVG 过滤器的 XML 文件的位置,并且可能包含指向特定
过滤元件。
另见HTML Living Standard 4.12.5.1.19 Filters 2017 年 10 月 3 日