123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import { ExcalidrawElement } from "../element/types";
- import { isTextElement } from "../element/typeChecks";
- import {
- getDiamondPoints,
- getArrowPoints,
- getLinePoints,
- } from "../element/bounds";
- import { RoughCanvas } from "roughjs/bin/canvas";
- import { Drawable } from "roughjs/bin/core";
- export function renderElement(
- element: ExcalidrawElement,
- rc: RoughCanvas,
- context: CanvasRenderingContext2D,
- ) {
- const generator = rc.generator;
- if (element.type === "selection") {
- const fillStyle = context.fillStyle;
- context.fillStyle = "rgba(0, 0, 255, 0.10)";
- context.fillRect(0, 0, element.width, element.height);
- context.fillStyle = fillStyle;
- } else if (element.type === "rectangle") {
- if (!element.shape) {
- element.shape = generator.rectangle(0, 0, element.width, element.height, {
- stroke: element.strokeColor,
- fill:
- element.backgroundColor === "transparent"
- ? undefined
- : element.backgroundColor,
- fillStyle: element.fillStyle,
- strokeWidth: element.strokeWidth,
- roughness: element.roughness,
- seed: element.seed,
- });
- }
- context.globalAlpha = element.opacity / 100;
- rc.draw(element.shape as Drawable);
- context.globalAlpha = 1;
- } else if (element.type === "diamond") {
- if (!element.shape) {
- const [
- topX,
- topY,
- rightX,
- rightY,
- bottomX,
- bottomY,
- leftX,
- leftY,
- ] = getDiamondPoints(element);
- element.shape = generator.polygon(
- [
- [topX, topY],
- [rightX, rightY],
- [bottomX, bottomY],
- [leftX, leftY],
- ],
- {
- stroke: element.strokeColor,
- fill:
- element.backgroundColor === "transparent"
- ? undefined
- : element.backgroundColor,
- fillStyle: element.fillStyle,
- strokeWidth: element.strokeWidth,
- roughness: element.roughness,
- seed: element.seed,
- },
- );
- }
- context.globalAlpha = element.opacity / 100;
- rc.draw(element.shape as Drawable);
- context.globalAlpha = 1;
- } else if (element.type === "ellipse") {
- if (!element.shape) {
- element.shape = generator.ellipse(
- element.width / 2,
- element.height / 2,
- element.width,
- element.height,
- {
- stroke: element.strokeColor,
- fill:
- element.backgroundColor === "transparent"
- ? undefined
- : element.backgroundColor,
- fillStyle: element.fillStyle,
- strokeWidth: element.strokeWidth,
- roughness: element.roughness,
- seed: element.seed,
- curveFitting: 1,
- },
- );
- }
- context.globalAlpha = element.opacity / 100;
- rc.draw(element.shape as Drawable);
- context.globalAlpha = 1;
- } else if (element.type === "arrow") {
- const [x1, y1, x2, y2, x3, y3, x4, y4] = getArrowPoints(element);
- const options = {
- stroke: element.strokeColor,
- strokeWidth: element.strokeWidth,
- roughness: element.roughness,
- seed: element.seed,
- };
- if (!element.shape) {
- element.shape = [
- // \
- generator.line(x3, y3, x2, y2, options),
- // -----
- generator.line(x1, y1, x2, y2, options),
- // /
- generator.line(x4, y4, x2, y2, options),
- ];
- }
- context.globalAlpha = element.opacity / 100;
- (element.shape as Drawable[]).forEach(shape => rc.draw(shape));
- context.globalAlpha = 1;
- return;
- } else if (element.type === "line") {
- const [x1, y1, x2, y2] = getLinePoints(element);
- const options = {
- stroke: element.strokeColor,
- strokeWidth: element.strokeWidth,
- roughness: element.roughness,
- seed: element.seed,
- };
- if (!element.shape) {
- element.shape = generator.line(x1, y1, x2, y2, options);
- }
- context.globalAlpha = element.opacity / 100;
- rc.draw(element.shape as Drawable);
- context.globalAlpha = 1;
- } else if (isTextElement(element)) {
- context.globalAlpha = element.opacity / 100;
- const font = context.font;
- context.font = element.font;
- const fillStyle = context.fillStyle;
- context.fillStyle = element.strokeColor;
- context.fillText(
- element.text,
- 0,
- element.baseline || element.actualBoundingBoxAscent || 0,
- );
- context.fillStyle = fillStyle;
- context.font = font;
- context.globalAlpha = 1;
- } else {
- throw new Error("Unimplemented type " + element.type);
- }
- }
|