Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function stringToKey(str: string) {
const strs = str.split(',')
return new Vec2(parseInt(strs[0]), parseInt(strs[1]))
}
boundingRect() {
const binaryWidth = Math.ceil(this.size.width / 32)
const binarySize = new Vec2(binaryWidth, this.size.height)
if (!binaryTexture.size.equals(binarySize)) {
binaryTexture.size = binarySize
}
drawVisibilityToBinary(binaryDrawTarget, this.texture)
const data = new Int32Array(binarySize.width * binarySize.height)
binaryDrawTarget.readPixels(new Rect(new Vec2(), binarySize), new Uint8Array(data.buffer))
return getBoundingRect(data, this.size)
}
boundingRect() {
drawVisibilityToBinary(binaryDrawTarget, this.texture)
binaryDrawTarget.readPixels(new Rect(new Vec2(), binaryTexture.size), new Uint8Array(binaryData.buffer))
return getBoundingRect(binaryData, new Vec2(Tile.width))
}
reset() {
if (this.picture) {
this.dimensionSelectViewModel.reset(this.picture.dimension)
this.dimensionSelectViewModel.unit = 'percent'
this.resetRect(new Rect(new Vec2(), this.picture.size))
}
}
drawTexture(src: Texture, opts: {transform: Transform, blendMode: BlendMode, bicubic?: boolean, srcRect?: Rect}) {
const {blendMode, transform, bicubic, srcRect} = opts
const rect = new Rect(new Vec2(), src.size).transform(transform)
for (const key of TiledTexture.keysForRect(rect)) {
tileDrawTarget.texture = this.get(key).texture
drawTexture(tileDrawTarget, src, {
transform: transform.translate(key.mulScalar(-Tile.width)),
blendMode,
bicubic,
srcRect
})
}
}
@action keyDown(ev: React.KeyboardEvent) {
super.keyDown(ev)
switch (ev.key) {
case "ArrowLeft":
this.translation = this.translation.add(new Vec2(-1, 0))
break
case "ArrowRight":
this.translation = this.translation.add(new Vec2(1, 0))
break
case "ArrowUp":
this.translation = this.translation.add(new Vec2(0, -1))
break
case "ArrowDown":
this.translation = this.translation.add(new Vec2(0, 1))
break
}
}
@action keyDown(ev: React.KeyboardEvent) {
super.keyDown(ev)
switch (ev.key) {
case "ArrowLeft":
this.translation = this.translation.add(new Vec2(-1, 0))
break
case "ArrowRight":
this.translation = this.translation.add(new Vec2(1, 0))
break
case "ArrowUp":
this.translation = this.translation.add(new Vec2(0, -1))
break
case "ArrowDown":
this.translation = this.translation.add(new Vec2(0, 1))
break
}
}
private readToThumbnail() {
this.texture.generateMipmap()
const rect = new Rect(new Vec2(0), this.thumbnailSize.mul(this.texture.size.div(this.originalSize)))
drawTexture(this.thumbnailDrawTarget, this.texture, {dstRect: rect, blendMode: "src"})
const {width, height} = rect
const data = new ImageData(width, height)
this.thumbnailDrawTarget.readPixels(rect, new Uint8Array(data.data.buffer))
this.thumbnailContext.putImageData(data, 0, 0)
}
updateCanvas() {
this.drawTarget.readPixels(new Rect(new Vec2(0), this.size), new Uint8Array(this.imageData.data.buffer))
this.context.putImageData(this.imageData, 0, 0)
}
loadTiledTexture(tiledTexture: TiledTexture) {
this.clear()
tiledTexture.drawToDrawTarget(this.drawTarget, {offset: new Vec2(0), blendMode: "src-over"})
this.readToThumbnail()
}