Canvas: Add vertical symbols

This commit is contained in:
Eric NGUYEN 2023-02-23 11:06:48 +01:00
parent 05eb5d5ef0
commit dcccfec1d2
2 changed files with 29 additions and 18 deletions

View file

@ -5,7 +5,6 @@ import { RenderSelector } from './Selector';
interface ISelectorProps { interface ISelectorProps {
symbols: Map<string, ISymbolModel> symbols: Map<string, ISymbolModel>
selected?: ISymbolModel selected?: ISymbolModel
scale?: number
} }
export function RenderSymbolSelector( export function RenderSymbolSelector(
@ -17,15 +16,20 @@ export function RenderSymbolSelector(
return; return;
} }
const scale = (props.scale ?? 1);
const [width, height] = [ const [width, height] = [
props.selected.width / scale, props.selected.width,
props.selected.height / scale props.selected.height
]; ];
const [x, y] = [ let x, y: number;
props.selected.offset + props.selected.width / 2,
-SYMBOL_MARGIN - height]; if (props.selected.isVertical) {
x = -SYMBOL_MARGIN - props.selected.width;
y = props.selected.offset;
} else {
x = props.selected.offset;
y = -SYMBOL_MARGIN - props.selected.height;
}
const text = props.selected.displayedText; const text = props.selected.displayedText;
RenderSelector( RenderSelector(
@ -37,7 +41,7 @@ export function RenderSymbolSelector(
y, y,
width, width,
height, height,
scale scale: 1
} }
); );
} }

View file

@ -1,12 +1,11 @@
import { type ISymbolModel } from '../../Interfaces/ISymbolModel'; import { type ISymbolModel } from '../../Interfaces/ISymbolModel';
import { DIMENSION_MARGIN, SYMBOL_MARGIN } from '../../utils/default'; import { SYMBOL_MARGIN } from '../../utils/default';
const IMAGE_CACHE = new Map<string, HTMLImageElement>(); const IMAGE_CACHE = new Map<string, HTMLImageElement>();
export function RenderSymbol( export function RenderSymbol(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
symbol: ISymbolModel, symbol: ISymbolModel): void {
scale: number): void {
const href = symbol.config.Image.Base64Image ?? symbol.config.Image.Url; const href = symbol.config.Image.Base64Image ?? symbol.config.Image.Url;
if (href === undefined) { if (href === undefined) {
@ -19,28 +18,36 @@ export function RenderSymbol(
newImage.src = href; newImage.src = href;
IMAGE_CACHE.set(href, newImage); IMAGE_CACHE.set(href, newImage);
newImage.onload = () => { newImage.onload = () => {
DrawImage(ctx, scale, newImage, symbol); DrawImage(ctx, newImage, symbol);
}; };
return; return;
} }
DrawImage(ctx, scale, image, symbol); DrawImage(ctx, image, symbol);
} }
function DrawImage( function DrawImage(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
scale: number,
image: HTMLImageElement, image: HTMLImageElement,
symbol: ISymbolModel symbol: ISymbolModel
): void { ): void {
let x, y: number;
if (symbol.isVertical) {
x = -SYMBOL_MARGIN - symbol.width;
y = symbol.offset;
} else {
x = symbol.offset;
y = -SYMBOL_MARGIN - symbol.height;
}
ctx.save(); ctx.save();
ctx.fillStyle = '#000000'; ctx.fillStyle = '#000000';
const width = symbol.width / scale; const width = symbol.width;
const height = symbol.height / scale; const height = symbol.height;
ctx.drawImage( ctx.drawImage(
image, image,
symbol.offset + symbol.width / 2, x,
-SYMBOL_MARGIN - height, y,
width, width,
height height
); );