@@ -42,6 +42,7 @@ import { RoughCanvas } from "roughjs/canvas";
4242import { Options } from "roughjs/core" ;
4343import rough from "roughjs/bin/rough" ;
4444import { getFontSize , getLineHeight } from "@/utils/textUtils" ;
45+ import { generateFreeDrawPath } from "./RenderElements" ;
4546
4647export class CanvasEngine {
4748 private canvas : HTMLCanvasElement ;
@@ -483,8 +484,8 @@ export class CanvasEngine {
483484 shape . roughStyle ,
484485 true
485486 ) ;
486- } else if ( shape . type === "pen " ) {
487- this . drawPencil (
487+ } else if ( shape . type === "free-draw " ) {
488+ this . drawFreeDraw (
488489 shape . points ,
489490 shape . strokeFill ,
490491 shape . bgFill ,
@@ -521,7 +522,7 @@ export class CanvasEngine {
521522
522523 mouseUpHandler = ( e : MouseEvent ) => {
523524 if (
524- this . activeTool !== "pen " &&
525+ this . activeTool !== "free-draw " &&
525526 this . activeTool !== "eraser" &&
526527 this . activeTool !== "line" &&
527528 this . activeTool !== "arrow"
@@ -675,13 +676,13 @@ export class CanvasEngine {
675676 } ;
676677 break ;
677678
678- case "pen " :
679+ case "free-draw " :
679680 const currentShape =
680681 this . existingShapes [ this . existingShapes . length - 1 ] ;
681- if ( currentShape ?. type === "pen " ) {
682+ if ( currentShape ?. type === "free-draw " ) {
682683 shape = {
683684 id : uuidv4 ( ) ,
684- type : "pen " ,
685+ type : "free-draw " ,
685686 points : currentShape . points ,
686687 strokeWidth : this . strokeWidth ,
687688 strokeFill : this . strokeFill ,
@@ -808,10 +809,10 @@ export class CanvasEngine {
808809 this . startX = x ;
809810 this . startY = y ;
810811
811- if ( this . activeTool === "pen " ) {
812+ if ( this . activeTool === "free-draw " ) {
812813 this . existingShapes . push ( {
813814 id : uuidv4 ( ) ,
814- type : "pen " ,
815+ type : "free-draw " ,
815816 points : [ { x, y } ] ,
816817 strokeWidth : this . strokeWidth ,
817818 strokeFill : this . strokeFill ,
@@ -927,12 +928,12 @@ export class CanvasEngine {
927928 ) ;
928929 break ;
929930
930- case "pen " :
931+ case "free-draw " :
931932 const currentShape =
932933 this . existingShapes [ this . existingShapes . length - 1 ] ;
933- if ( currentShape ?. type === "pen " ) {
934+ if ( currentShape ?. type === "free-draw " ) {
934935 currentShape . points . push ( { x, y } ) ;
935- this . drawPencil (
936+ this . drawFreeDraw (
936937 currentShape . points ,
937938 this . strokeFill ,
938939 this . bgFill ,
@@ -1211,7 +1212,7 @@ export class CanvasEngine {
12111212
12121213 return distance <= tolerance && withinLineBounds ;
12131214 }
1214- case "pen " : {
1215+ case "free-draw " : {
12151216 return shape . points . some (
12161217 ( point ) => Math . hypot ( point . x - x , point . y - y ) <= tolerance
12171218 ) ;
@@ -1604,7 +1605,7 @@ export class CanvasEngine {
16041605 }
16051606 }
16061607
1607- drawPencil (
1608+ drawFreeDraw (
16081609 points : { x : number ; y : number } [ ] ,
16091610 strokeFill : string ,
16101611 bgFill : string ,
@@ -1613,22 +1614,31 @@ export class CanvasEngine {
16131614 strokeWidth : StrokeWidth
16141615 ) {
16151616 if ( ! points . length ) return ;
1617+
1618+ // const svgPathData = generateFreeDrawPath(points, strokeWidth);
1619+
16161620 if ( fillStyle === "solid" ) {
1617- this . ctx . beginPath ( ) ;
1618- this . ctx . strokeStyle = strokeFill ;
1619- this . ctx . lineWidth = strokeWidth ;
1620- this . ctx . setLineDash (
1621- strokeStyle === "dashed"
1622- ? getDashArrayDashed ( strokeWidth )
1623- : strokeStyle === "dotted"
1624- ? getDashArrayDotted ( strokeWidth )
1625- : [ ]
1626- ) ;
1627- if ( points [ 0 ] === undefined ) return null ;
1628- this . ctx . moveTo ( points [ 0 ] . x , points [ 0 ] . y ) ;
1629- points . forEach ( ( point ) => this . ctx . lineTo ( point . x , point . y ) ) ;
1630- this . ctx . stroke ( ) ;
1621+ const path = new Path2D ( generateFreeDrawPath ( points , strokeWidth ) ) ;
1622+
1623+ this . ctx . save ( ) ;
1624+ this . ctx . fillStyle = strokeFill ;
1625+ this . ctx . fill ( path ) ;
1626+
1627+ if ( strokeStyle === "dashed" || strokeStyle === "dotted" ) {
1628+ this . ctx . strokeStyle = strokeFill ;
1629+ this . ctx . lineWidth = 1 ;
1630+ this . ctx . setLineDash (
1631+ strokeStyle === "dashed"
1632+ ? getDashArrayDashed ( 1 )
1633+ : getDashArrayDotted ( 1 )
1634+ ) ;
1635+ this . ctx . stroke ( path ) ;
1636+ this . ctx . setLineDash ( [ ] ) ;
1637+ }
1638+
1639+ this . ctx . restore ( ) ;
16311640 } else {
1641+ // // For rough/sketchy style, use the existing implementation
16321642 const pathStr = points . reduce (
16331643 ( path , point , index ) =>
16341644 path +
@@ -1637,6 +1647,7 @@ export class CanvasEngine {
16371647 : ` L ${ point . x } ${ point . y } ` ) ,
16381648 ""
16391649 ) ;
1650+
16401651 const options = this . getRoughOptions (
16411652 strokeWidth ,
16421653 strokeFill ,
@@ -1646,6 +1657,19 @@ export class CanvasEngine {
16461657 fillStyle
16471658 ) ;
16481659 this . roughCanvas . path ( pathStr , options ) ;
1660+
1661+ // For rough/sketchy style, use the improved path with rough.js
1662+ // const options = this.getRoughOptions(
1663+ // strokeWidth,
1664+ // strokeFill,
1665+ // 0,
1666+ // bgFill,
1667+ // "solid",
1668+ // fillStyle
1669+ // );
1670+
1671+ // // Use the SVG path data from perfect-freehand with rough.js
1672+ // this.roughCanvas.path(svgPathData, options);
16491673 }
16501674 }
16511675
0 commit comments