Skip to content
Snippets Groups Projects
Unverified Commit bbbd86a5 authored by Alexander Harkness's avatar Alexander Harkness
Browse files

Combine pressure and radius to single radius value.

Radius smoothing now difference relative to the max-of-2 radiii instead of absolute values.
parent 44b8b2c4
No related branches found
No related tags found
1 merge request!45Brush size
Pipeline #103566 passed
...@@ -260,9 +260,8 @@ canvas.input.addEventListener("strokestart", ({ detail: e }) => { ...@@ -260,9 +260,8 @@ canvas.input.addEventListener("strokestart", ({ detail: e }) => {
e.pointerId, e.pointerId,
room.addPath([ room.addPath([
...mousePos, ...mousePos,
e.pressure, canvas.getStrokeRadius(e.pressure),
canvas.getStrokeColour(), canvas.getStrokeColour(),
canvas.stroke_radius,
]), ]),
) )
} else if (currentTool == tools.ERASER) { } else if (currentTool == tools.ERASER) {
...@@ -284,9 +283,8 @@ canvas.input.addEventListener("strokemove", ({ detail: e }) => { ...@@ -284,9 +283,8 @@ canvas.input.addEventListener("strokemove", ({ detail: e }) => {
if (currentTool == tools.PEN) { if (currentTool == tools.PEN) {
room.extendPath(pathIDsByPointerID.get(e.pointerId), [ room.extendPath(pathIDsByPointerID.get(e.pointerId), [
...mousePos, ...mousePos,
e.pressure, canvas.getStrokeRadius(e.pressure),
canvas.getStrokeColour(), canvas.getStrokeColour(),
canvas.stroke_radius,
]) ])
} else if (currentTool == tools.ERASER) { } else if (currentTool == tools.ERASER) {
erasePoint(mousePos) erasePoint(mousePos)
......
...@@ -56,7 +56,8 @@ const smoothPath = ([...path]) => { ...@@ -56,7 +56,8 @@ const smoothPath = ([...path]) => {
const dx = path[i][0] - path[i - 1][0] const dx = path[i][0] - path[i - 1][0]
const dy = path[i][1] - path[i - 1][1] const dy = path[i][1] - path[i - 1][1]
const dw = path[i][2] - path[i - 1][2] const dw = path[i][2] - path[i - 1][2]
const segmentsToSplit = Math.ceil(dw / MAX_PRESSURE_DELTA) const normdw = dw / Math.max(0.05, Math.max(path[i][2], path[i - 1][2]))
const segmentsToSplit = Math.ceil(normdw / MAX_PRESSURE_DELTA)
const newPoints = [] const newPoints = []
for (let j = 1; j < segmentsToSplit; j++) { for (let j = 1; j < segmentsToSplit; j++) {
newPoints.push([ newPoints.push([
...@@ -73,13 +74,6 @@ const smoothPath = ([...path]) => { ...@@ -73,13 +74,6 @@ const smoothPath = ([...path]) => {
return path return path
} }
const getStrokeRadius = (pressure, radius) => {
return (
MIN_STROKE_RADIUS +
(radius + pressure) * (MAX_STROKE_RADIUS - MIN_STROKE_RADIUS)
)
}
export const input = new EventTarget() export const input = new EventTarget()
const createPathElem = (d, width) => { const createPathElem = (d, width) => {
...@@ -92,14 +86,12 @@ const createPathElem = (d, width) => { ...@@ -92,14 +86,12 @@ const createPathElem = (d, width) => {
export const renderPath = (id, points) => { export const renderPath = (id, points) => {
points = points.filter(([x]) => x != null) points = points.filter(([x]) => x != null)
let colour = "" let colour = ""
let radius = 0
// Split up points into completely non-erased segments. // Split up points into completely non-erased segments.
let segments = [[]] let segments = [[]]
for (const point of points) { for (const point of points) {
colour = point[3] colour = point[3]
radius = point[4] if (point[4] != false) {
if (point[5] != false) {
segments[segments.length - 1].push(point) segments[segments.length - 1].push(point)
} else { } else {
segments.push([]) segments.push([])
...@@ -136,8 +128,7 @@ export const renderPath = (id, points) => { ...@@ -136,8 +128,7 @@ export const renderPath = (id, points) => {
circleElem.setAttribute("fill", colour) circleElem.setAttribute("fill", colour)
circleElem.setAttribute("cx", subpath[0][0]) circleElem.setAttribute("cx", subpath[0][0])
circleElem.setAttribute("cy", subpath[0][1]) circleElem.setAttribute("cy", subpath[0][1])
circleElem.setAttribute("r", getStrokeRadius(subpath[0][2], radius)) circleElem.setAttribute("r", subpath[0][2])
console.log(getStrokeRadius(subpath[0][2], radius))
pathGroupElem.appendChild(circleElem) pathGroupElem.appendChild(circleElem)
} else { } else {
// Further split up segments based on thickness. // Further split up segments based on thickness.
...@@ -146,17 +137,13 @@ export const renderPath = (id, points) => { ...@@ -146,17 +137,13 @@ export const renderPath = (id, points) => {
for (let i = 1; i < subpath_.length; i++) { for (let i = 1; i < subpath_.length; i++) {
if (subpath_[i][2] != w) { if (subpath_[i][2] != w) {
const d = lineFn([...subpath_.splice(0, i), subpath_[0]]) const d = lineFn([...subpath_.splice(0, i), subpath_[0]])
pathGroupElem.appendChild( pathGroupElem.appendChild(createPathElem(d, w * 2))
createPathElem(d, getStrokeRadius(w, radius) * 2),
)
w = subpath_[0][2] w = subpath_[0][2]
i = 1 i = 1
} }
} }
const d = lineFn(subpath_) const d = lineFn(subpath_)
pathGroupElem.appendChild( pathGroupElem.appendChild(createPathElem(d, w * 2))
createPathElem(d, getStrokeRadius(w, radius) * 2),
)
} }
} }
} }
...@@ -196,3 +183,14 @@ export function getStrokeColour() { ...@@ -196,3 +183,14 @@ export function getStrokeColour() {
export function setStrokeRadius(radius) { export function setStrokeRadius(radius) {
stroke_radius = radius stroke_radius = radius
} }
const calculateStrokeRadius = (pressure, radius) => {
return (
MIN_STROKE_RADIUS +
(radius + pressure) * (MAX_STROKE_RADIUS - MIN_STROKE_RADIUS)
)
}
export function getStrokeRadius(pressure) {
return calculateStrokeRadius(pressure, stroke_radius)
}
...@@ -23,14 +23,14 @@ class Room extends EventTarget { ...@@ -23,14 +23,14 @@ class Room extends EventTarget {
this._y.destroy() this._y.destroy()
} }
addPath([x, y, w, colour, radius]) { addPath([x, y, w, colour]) {
const id = uuidv4() const id = uuidv4()
this._y.share.strokeAdd.set(id, Y.Array).push([[x, y, w, colour, radius]]) this._y.share.strokeAdd.set(id, Y.Array).push([[x, y, w, colour]])
return id return id
} }
extendPath(id, [x, y, w, colour, radius]) { extendPath(id, [x, y, w, colour]) {
this._y.share.strokeAdd.get(id).push([[x, y, w, colour, radius]]) this._y.share.strokeAdd.get(id).push([[x, y, w, colour]])
} }
getPaths() { getPaths() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment