我正在try 使用CSS Paint API设置最基本的Reaction示例.

我没有编译错误,但示例没有显示.

内页index.html张:

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

public/paintWorklet.js:

class Shape {
  paint(ctx, geom, properties) {
    ctx.strokeStyle = 'red';
    ctx.lineWidth = 4;
    ctx.beginPath();
    ctx.arc(200, 200, 50, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.closePath();
  }
}

registerPaint('pattern', Shape);

App.tsx:

import React from 'react';
import './App.css';

const TestDiv = () => {  
  return(
    <div className='TestDiv'>
      <h1>I am a TestDiv</h1>
    </div>
  )
}

function App() {
  return (
    <div className="App">
      <TestDiv />
    </div>
  );
}

export default App;

App.css:

.TestDiv {
  background: paint(pattern);
}

到目前为止,我已经try 在src中移动该工作面板,将其放在一个useEffect中,但仍然没有成功.

尽管这个index.html内的代码片段可以正常工作.

 <style>
  body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: paint(pattern);
  }
</style>

推荐答案

我发现问题在于:

  • 你的TestDiv有一个基于内容的宽度和高度.
  • 您绘制的圆超出了TestDiv个维度,因此您将看不到该圆.

解决方案:

  • 使TestDiv更大或更大
  • 使circle更小以适合div

enter image description here

demo

Css相关问答推荐

CSS 中的 :not(style) ~ :not(style) 是什么?

解释 "document.styleSheets[0].cssRules[0].style;"

混合网格自动布局与固定的行列位置

应用填充时边框无法正常工作

Angular 以Angular 获取当前聚焦的元素

为什么同一个 CSS 类连续出现两次

父母是内联块,子元素有%填充=奇怪的行为

tailwind css 中的自定义组状态

为什么过渡属性不适用于 box-shadow?

图像 3D 悬停效果

为什么 em 不将定义的字体大小加倍?

你如何调试可打印的 CSS?

如何制作一个div来包装两个浮动div?

为什么我们将