【问题标题】:Can't import css houdini paint js file无法导入css houdini paint js文件
【发布时间】: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


【解决方案1】:

addModule 函数不能通过 webpack 或其他打包器工作,而是通过浏览器的原生模块系统工作。您必须将testPaint.js 文件放在公共目录中,否则它会与其他所有内容捆绑在一起。

这是我添加到 index.html 以使其从本地 Create React App 项目的公共目录中运行的内容:

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

我实际上并没有设置任何标记,只是添加了容器类来测试它:


如果您想在不通过公用文件夹的情况下使用它(因为通常您必须在更改公用目录的 index.html 中添加绘图模块),那么我建议使用 @ 987654322@ 设置脚本标签。需要注意的是,CRA 的热重载功能似乎会阻止脚本更新,因此每次更改脚本标签时,都需要手动刷新页面。

import React from 'react';
import { render } from 'react-dom';
import { Helmet } from 'react-helmet';
import styled from 'styled-components';

// Render these styled components like normal react components.
// They will pass on all props and work
// like normal react components – except they're styled!
const Demo = styled.div`
  background: #1108a0;
  padding: 50px 0;
`;
const Test = styled.div`
  --color: cyan;
  --multiplier: 0.24;
  --pad: 30;
  --slant: 20;
  background: paint(background-canvas);
  transition: --multiplier 0.4s;
  font: bold 6em sans-serif;
  color: yellow;
  text-shadow: 0 3px 1px cyan;
  line-height: 1.5em;
  width: max-content;
  padding-left: 30px;
  padding-right: 50px;
  isolation: isolate;
  &:hover {
    --multiplier: 1;
  }
  & span {
    mix-blend-mode: exclusion;
  }
`;
export const App = () => (
  <Demo>
    <Test className="el" right={'right'}>
      <span>JS-in-CSS</span>
    </Test>
  </Demo>
);
export const Helm = () => (
  <Helmet>
    <script language="javascript+paint">{`
  registerPaint('background-canvas', class {
  static get inputProperties() {
      return ['--multiplier', '--color', '--pad', '--slant'];
  }
  paint(ctx, geom, properties) {
    let multiplier = +properties.get('--multiplier').toString();
    let c = properties.get('--color').toString();
    let pad = +properties.get('--pad').toString();
    let slant = +properties.get('--slant').toString();
    ctx.moveTo(0, 0);
    ctx.lineTo(pad + (geom.width - slant - pad) * multiplier, 0);
    ctx.lineTo(pad + (geom.width - slant - pad) * multiplier + slant, geom.height);
    ctx.lineTo(0, geom.height);
    ctx.fillStyle = c;
    ctx.fill();
  }
})
`}</script>

    <script>{`
  if ("paintWorklet" in CSS) {
  const src = document.querySelector('script[language$="paint"]').innerHTML;
  const blob = new Blob([src], {
    type: 'text/javascript'
  });
  CSS.paintWorklet.addModule(URL.createObjectURL(blob));
}
`}</script>
  </Helmet>
);
render(
  <div>
    <Helm />
    <App />
  </div>,
  document.getElementById('root')
);

【讨论】:

  • 我猜这是有道理的。有没有办法解决?将该代码放在public 目录中并不理想...
  • @IronWaffleMan,我添加了一种避免公共目录的方法。我不太确定使用公共目录有什么问题。这就是它的用途:)
【解决方案2】:

如果您希望避免使用 /public,则可以使用像 Yarn Workspaces 这样的 monorepo 管理器将您的工作集保存在专用包中。然后,您可以像导入任何其他包依赖项一样将其导入您的客户端应用程序。

一步一步(使用 Yarn 工作区):

  1. 为您的应用(package.json 名称为 @packages/myapp)和工作包 (@packages/myworklet) 创建专用包
  2. 将您的工作集添加为应用程序的依赖项 -- 从 @packages/myapp 运行 yarn add @packages/myworklet
  3. 从@packages/myworklet 导入你的worklet 并添加它——见https://houdini.how/usage/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2021-09-21
    • 2021-05-15
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多