草案
此页面不完整。

PaintWorklet.registerPaint() 方法在 PaintWorklet interface registers a class programmatically generate an image where a CSS property expects a file.

句法

registerPaint(name, class);
					

参数

名称

The name of the worklet class to register.

class

A reference to the class that implements the worklet.

返回值

undefined

异常

TypeError

Thrown when one of the arguments is invalid or missing.

InvalidModificationError

Thrown when the a worklet already exists with the specified name.

范例

The following shows registering an example worklet module. This should be in a separate js file. Note that registerPaint() is called without a reference to PaintWorklet . The file itself is loaded through CSS.paintWorklet.addModule() (documented here on the parent class of PaintWorklet, at Worklet.addModule() .

/* checkboardWorklet.js */
class CheckerboardPainter {
  paint(ctx, geom, properties) {
    // Use `ctx` as if it was a normal canvas
    const colors = ['red', 'green', 'blue'];
    const size = 32;
    for(let y = 0; y < geom.height/size; y++) {
      for(let x = 0; x < geom.width/size; x++) {
        const color = colors[(x + y) % colors.length];
        ctx.beginPath();
        ctx.fillStyle = color;
        ctx.rect(x * size, y * size, size, size);
        ctx.fill();
      }
    }
  }
}
// Register our class under a specific name
registerPaint('checkerboard', CheckerboardPainter);
					

The first step in using a paintworket is defining the paint worklet using the registerPaint() function, as done above. To use it, you register it with the CSS.paintWorklet.addModule() 方法:

<script>
   CSS.paintWorklet.addModule('checkboardWorklet.js');
</script>
					

You can then use the paint() CSS function in your CSS anywhere an <image> value is valid.

li {
   background-image: paint(checkerboard);
}
					

规范

规范 状态 注释
CSS Painting API Level 1
The definition of 'PaintWorklet.registerPaint' in that specification.
工作草案 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

No compatibility data found. Please contribute data for "api.PaintWorklet.registerPaint" (depth: 1) to the MDN 兼容性数据存储库 .

另请参阅

元数据

  • 最后修改: