-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
165 lines (138 loc) · 3.47 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import Konva from 'konva'
import { imageToDataUrl } from '../utils/image-to-url'
import { createImageFromUrl } from '../utils/create-image-from-url'
import { Board } from '../Board'
import { ImageModel } from '../shape/models/ImageModel'
import { RectModel } from '../shape/models/RectModel'
import type { BackgroundOptions } from '../types'
export class Background {
/**
* Represents the background [[Image | image]]
*/
public image: ImageModel
/**
* Represents the background [[Rect | overlay]]
*/
public overlay: RectModel
/**
* Represents the [[Board]]
*/
private board: Board
/**
* Creates a new background image and overlay
*
* @param board The [[Board]]
*/
constructor(board: Board) {
this.board = board
const image = new Konva.Image({
image: undefined
})
const overlay = new Konva.Rect()
this.image = new ImageModel(board, image, {
selectable: false,
history: false
})
this.overlay = new RectModel(board, overlay, {
selectable: false,
history: false
})
}
/**
* Returns background nodes which includes image and overlay
*/
public get nodes() {
return [this.image.node, this.overlay.node]
}
/**
* Returns current position of background
*/
public getPosition() {
return {
x: this.image.node.x() || this.overlay.node.x(),
y: this.image.node.y() || this.overlay.node.y()
}
}
/**
* Loads the background image from given file
*
* @param file The image file
* @param options The background options
*/
public async setImageFromFile(
file: File,
options?: Partial<BackgroundOptions>
) {
const url = await imageToDataUrl(file)
this.setImageFromUrl(url, options)
}
/**
* Loads the background image from url
*
* @param url The image url
* @param options The background options
*/
public async setImageFromUrl(
url: string,
options: Partial<BackgroundOptions> = {
size: 'resize'
}
) {
this.board.history.create(
this.board.layer,
[this.board.stage, this.image.node, this.overlay.node],
this.board.rescale.bind(this)
)
const background = await createImageFromUrl(url)
let width = background.width()
let height = background.height()
const ratios = {
width: this.board.stage.width() / width,
height: this.board.stage.height() / height
}
if (options.size === 'cover') {
const ratio = Math.max(ratios.width, ratios.height)
width *= ratio
height *= ratio
}
if (options.size === 'contain') {
const ratio = Math.min(ratios.width, ratios.height)
width *= ratio
height *= ratio
}
if (options.size === 'resize') {
this.board.stage.setAttrs({
width,
height
})
}
if (options.size === 'stretch') {
width = this.board.stage.width()
height = this.board.stage.height()
}
this.image.node.setAttrs({
width,
height,
image: background.image(),
x: options.x ?? background.x(),
y: options.y ?? background.y()
})
this.overlay.node.setAttrs({
width,
height
})
}
/**
* Updates color of overlay
*
* @param color The given color in Hex or RGB
*/
public fill(color: string) {
this.board.history.create(this.board.layer, this.overlay.node)
this.overlay.node.setAttrs({
width: this.board.stage.width(),
height: this.board.stage.height(),
fill: color
})
}
}