Skip to content

Applying an SVG filter to the canvas #157

@velara3

Description

@velara3

The documentation says, "Skia canvas supports the full set of CSS filter image processing operators"

In the examples I've seen applying a filter is as simple as setting the filter property:

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.filter = "blur(4px)";

The CSS filter documentation mentions the URL() filter:

A CSS url(). Takes an IRI pointing to an SVG filter element, which may be embedded in an external XML file.

How would I do the same in Skia Canvas?

Could it be embedded? Will it load an external file?

Embedded:

canvas.filter = url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E");

External:

canvas.filter = url("filter.svg#hueRotate");

// filter.svg: 
<?xml version="1.0" standalone="no"?>
<svg width="1" height="1" version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
    <filter id="hueRotate">
      <feColorMatrix type="hueRotate" values="270"/>
    </filter>
  </defs>
</svg>

I realize now I can probably test this. However, I didn't find any examples in the documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions