Skip to content

Commit

Permalink
color picker + misc bug fix (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenist authored Dec 15, 2023
1 parent 45052f0 commit 10033f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
20 changes: 14 additions & 6 deletions libs/drawing-engine/src/engine/DrawingEngine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TextureDrawingProgram } from "../programs/TextureDrawingProgram"
import type { Vec2 } from "@libs/shared"
import { Color } from "@libs/shared"
import { Layer } from "./Layer"
import { Layer, LayerSettings } from "./Layer"
import { SourceImage } from "../utils/image/SourceImage"
import { ToolName, ToolNames } from "../exports"
import { LineHistoryEntry, LineTool } from "../tools/LineTool"
Expand Down Expand Up @@ -89,8 +89,8 @@ export class DrawingEngine {
prevTool: defaultTool,
}

this.savedDrawingLayer = new Layer(gl)
this.activeDrawingLayer = new Layer(gl, { clearBeforeDrawing: true })
this.savedDrawingLayer = this.makeLayer()
this.activeDrawingLayer = this.makeLayer({ clearBeforeDrawing: true })

this.program = new TextureDrawingProgram(gl, this.state.pixelDensity)

Expand All @@ -103,6 +103,10 @@ export class DrawingEngine {
this.callListeners("changeTool", { tool: this.state.tool })
}

private makeLayer(options?: Partial<LayerSettings>) {
return new Layer(this.gl, options)
}

public get pixelDensity() {
return this.state.pixelDensity
}
Expand Down Expand Up @@ -138,7 +142,6 @@ export class DrawingEngine {
}

public setColor(color: Color) {
this.commitToSavedLayer()
this.state.color = color
}

Expand Down Expand Up @@ -187,9 +190,14 @@ export class DrawingEngine {
return position[0] >= 0 && position[0] <= this.state.width && position[1] >= 0 && position[1] <= this.state.height
}

public draw(drawCallback: () => void): this {
public draw(drawCallback: () => HistoryItem | undefined): this {
const layer = this.activeDrawingLayer
this.program.createTextureImage(layer, drawCallback)
this.program.createTextureImage(layer, () => {
const drawData = drawCallback()
if (drawData) {
this.callListeners("draw", drawData)
}
})
this.render()
return this
}
Expand Down
6 changes: 3 additions & 3 deletions libs/drawing-engine/src/tools/LineTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class LineTool {
}
const path = this.currentPath
this.engine.draw(() => {
const pressure = this.options.pressureEnabled ? path.map(([, , pressure]) => pressure ?? 1.0) : undefined
const pressure = this.options.pressureEnabled ? path.map(([, , pressure]) => pressure ?? 0.0) : undefined
this.program.draw(
{
points: path.map(([x, y]) => [x, y]).flat(),
Expand All @@ -120,8 +120,8 @@ export class LineTool {
})
}

private hasPressure([_, ...points]: number[]) {
return !points.some((point) => point === 0)
private hasPressure(points: number[]) {
return points.some((point) => point !== 0)
}

protected getLineOptions(): Required<Omit<DrawLineOptions, "drawType">> {
Expand Down

0 comments on commit 10033f2

Please sign in to comment.