【发布时间】:2021-02-20 23:21:56
【问题描述】:
我有一个 React/Electron 应用程序,我正在尝试使用新的 CSS Houdini paint() 函数(如 in this page 所示)。在我的项目的index.html 文件中,我添加了一个带有paintWorklet addModule() 函数的脚本标签,如图所示:
<script>
CSS.paintWorklet.addModule('../src/ResultDisplay/DefaultResultDisplay/testPaint.js');
</script>
然后在那个testPaint.js 文件中,我基本上拥有该博客文章中显示的内容的副本:
registerPaint(
"testPaint",
class {
paint(ctx, geom) {
console.log("painting!!!");
const circleSize = 10;
const bodyWidth = geom.width;
const bodyHeight = geom.height;
const maxX = Math.floor(bodyWidth / circleSize);
const maxY = Math.floor(bodyHeight / circleSize);
for (let y = 0; y < maxY; y++) {
for (let x = 0; x < maxX; x++) {
ctx.fillStyle = "blue";
ctx.beginPath();
ctx.arc(
x * circleSize * 2 + circleSize,
y * circleSize * 2 + circleSize,
circleSize,
0,
2 * Math.PI,
true
);
ctx.closePath();
ctx.fill();
}
}
}
}
);
最后是我的 css 文件:
.container {
background-image: paint(testPaint);
display: flex;
margin: 4px;
border-radius: 12px;
height: 75px;
}
我应该指出我正在使用 CSS 模块,所以这个文件是 defaultResultStyles.module.scss;不确定这是否会影响任何事情。当我在我的应用程序中调出本应具有这些样式的组件时,它没有样式,尽管检查它,它确实显示 background-image: paint(testPaint). The console.log that I added to the testPaint.js` 文件从未显示。
我尝试了addModule 文件路径的多种变体;我试过testPaint.js,以./src 和src 开头,但似乎没有任何效果;这在 Electron/React 应用程序中是否可行?
【问题讨论】:
-
您可以将文件与其他所有内容一起放入您的 src 目录中,然后将其解析为 URL,而不是像
import angledCorners from '!file-loader!./css-paint/angled-corners';那样像平常一样编译它——这是 wepack 的一个功能。然后你可以添加它CSS.paintWorklet.addModule(angledCorners);,但据我所知......我可以看到文件正在加载,但它没有绘画。
标签: css reactjs electron css-houdini css-paint-api