This commit is contained in:
Maciej Caderek
2022-01-22 06:00:40 +01:00
parent 6112c00dec
commit ec7a11c9b6
20 changed files with 262 additions and 139 deletions

View File

@@ -1,6 +1,6 @@
import { SquareStyle } from "../../types";
import createGradient from "../fill/createGradient";
import createPattern from "../fill/createPattern";
import loadImage from "../loaders/loadImage";
const drawRectangle = async (
ctx: CanvasRenderingContext2D,
@@ -10,11 +10,15 @@ const drawRectangle = async (
y: number,
squareStyle: SquareStyle
) => {
if (squareStyle.type === "image") {
const img = await loadImage(squareStyle.data.src);
ctx.drawImage(img, x, y, width, height);
return;
}
const fill = await (squareStyle.type === "solid"
? squareStyle.data.color
: squareStyle.type === "gradient"
? createGradient(ctx, squareStyle.data, width, height, x, y)
: createPattern(ctx, squareStyle.data));
: createGradient(ctx, squareStyle.data, width, height, x, y));
if (fill === null) {
throw new Error("Cannot create canvas fill style");