11import {
22 BooleanParameter ,
33 TreeItem ,
4+ Vec2 ,
45 Vec3 ,
56 XRPoseEvent ,
67 Xfo ,
@@ -22,8 +23,11 @@ import { AppData } from '../../../types/types'
2223 * @extends CreateLineTool
2324 */
2425class CreateFreehandLineTool extends CreateLineTool {
25- invXfo : Xfo
26- prevP : Vec3
26+ private invXfo : Xfo
27+ private prevPointerPos : Vec2
28+
29+ drawLineOnSurface = true
30+
2731 /**
2832 * Create a create freehand line tool.
2933 *
@@ -34,15 +38,15 @@ class CreateFreehandLineTool extends CreateLineTool {
3438 }
3539
3640 screenPosToXfo ( event : ZeaMouseEvent | ZeaTouchEvent ) : Xfo {
37- return super . screenPosToXfo ( event , true )
41+ return super . screenPosToXfo ( event , this . drawLineOnSurface )
3842 }
3943
4044 /**
4145 * Starts the creation of a free hand line.
4246 *
4347 * @param xfo - The xfo param.
4448 */
45- createStart ( xfo : Xfo ) : void {
49+ createStart ( xfo : Xfo , event : ZeaPointerEvent ) : void {
4650 const color = this . colorParam . value
4751 const lineThickness = this . lineThickness . value
4852
@@ -52,7 +56,7 @@ class CreateFreehandLineTool extends CreateLineTool {
5256 this . xfo = xfo
5357 this . invXfo = xfo . inverse ( )
5458 this . stage = 1
55- this . prevP = xfo . tr
59+ this . prevPointerPos = event . pointerPos
5660 this . length = 0
5761 }
5862
@@ -62,14 +66,20 @@ class CreateFreehandLineTool extends CreateLineTool {
6266 * @param pt - The pt param.
6367 */
6468 createMove ( pt : Vec3 , event : ZeaPointerEvent ) : void {
69+ const pointerPos = event . pointerPos
70+
6571 const p = this . invXfo . transformVec3 ( pt )
66- const delta = p . subtract ( this . prevP ) . length ( )
67- this . change . update ( {
68- point : p ,
69- } )
72+ const delta = pointerPos . distanceTo ( this . prevPointerPos )
73+ // Add a point only if the distance is greater than 5 pixels.
74+ // This is to avoid adding too many points when the mouse is moved slowly.
75+ if ( delta > 5 ) {
76+ this . change . update ( {
77+ point : p ,
78+ } )
7079
71- this . length += delta
72- this . prevP = p
80+ this . length += delta
81+ this . prevPointerPos = pointerPos
82+ }
7383 }
7484
7585 /**
0 commit comments