[英]how to load Image and use it in canvas with gatsby SSR
问题是我无法使用gatsby在我的SSR应用程序中创建不支持任何类型的图像: CSSImageValue
或HTMLImageElement
或SVGImageElement
或HTMLVideoElement
或HTMLCanvasElement
或ImageBitmap
或OffscreenCanvas
。
我尝试使用canvas
npm包Canvas.Image
来制作图像HTMLImageElement,但它与Image构造函数消息也有相同的错误。
我的主要错误消息:
Context.js:225 Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'
节点canvas
程序包代码和错误消息:
import { Image as CanvasImage } from 'canvas';
const image = new CanvasImage();
image.src = avatars[avatar];
console.log(image);
TypeError: canvas__WEBPACK_IMPORTED_MODULE_3__.Image is not a constructor
我使用react-konva
包在其中绘制画布和Image
。
终于我有了办法。 我创建了一个名为getImage
的函数, loadImage
使用Promise
从canvas
loadImage
方法中加载图像源:
import { loadImage } from 'canvas';
export async function getImage(src, cb) {
return loadImage(src)
.then((image) => image)
.catch(err => console.log(err));
};
并轻松地在我的组件中使用它:
let [imageSource, setImageSource] = useState();
useEffect(() => {
getImage(ladderSvg)
.then((image) => setImageSource(image))
} , []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.