1+ import { IModel } from "@e-board/board-core" ;
2+ import { BaseRender } from "../../baseElement/baseRender" ;
3+ import { IPictureModel } from "../type" ;
4+
5+ class Render extends BaseRender < IPictureModel > {
6+ private transformService = this . board . getService ( 'transformService' )
7+ private renderService = this . board . getService ( 'renderService' )
8+ private imageCache = new Map < string , HTMLImageElement > ( ) ;
9+ public transformPoint ( point : { x : number ; y : number } , inverse = false ) {
10+ return this . transformService . transformPoint ( point , inverse ) ;
11+ }
12+ public render = ( model : IModel < IPictureModel > , _ : any , useWorldCoords = false ) => {
13+ const context = this . board . getCtx ( ) ;
14+ if ( ! context || ! model . imageData || ! model . points ) return ;
15+
16+ context . save ( ) ;
17+
18+ let img = this . imageCache . get ( model . id ) ;
19+ if ( ! img ) {
20+ img = new Image ( ) ;
21+ img . src = model . imageData ;
22+ this . imageCache . set ( model . id , img ) ;
23+ }
24+
25+ if ( img . complete ) {
26+ const zoom = this . transformService . getView ( ) . zoom ;
27+ const transformedPos = useWorldCoords ? model . points [ 0 ] : this . transformPoint ( model . points [ 0 ] ) ;
28+ const width = useWorldCoords ? ( model . width || img . width ) : ( model . width || img . width ) * zoom ;
29+ const height = useWorldCoords ? ( model . height || img . height ) : ( model . height || img . height ) * zoom ;
30+
31+ // 将图片中心对齐到指定位置,而不是左上角
32+ const drawX = transformedPos . x
33+ const drawY = transformedPos . y
34+
35+ context . drawImage ( img , drawX , drawY , width , height ) ;
36+ } else {
37+ img . onload = ( ) => {
38+ this . renderService . reRender ( ) ;
39+ } ;
40+ }
41+
42+ context . restore ( ) ;
43+ } ;
44+ }
45+
46+
47+ export { Render }
0 commit comments