### CNCBuildCanvas Configuration Options Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Provides an example of a comprehensive configuration object for CNCBuildCanvas, covering display, colors, platform, and toolhead settings.
```javascript
var options = {
// Display options
drawDimensions: true,
measurementUnit: "mm",
endcapSize: 0.25,
// Colors (hex strings without # prefix)
toolEnabledColor: "FFFFFF", // White for active tool
toolDisabledColor: "999999", // Gray for inactive tool
toolHistoryColor: "0033CC", // Blue for tool history
platformColor: "0088FF", // Light blue for platform
dimensionColor: "0088FF", // Light blue for dimensions
// Platform configuration
platform: {
grid: true, // Enable grid display
outline: true, // Show platform edges
size: [300, 300], // 300mm x 300mm platform
ruleIncrement: [25, 25] // 25mm grid spacing
},
// Toolhead options
toolhead: {
enabled: true
}
};
var canvas = $("#canvasDiv").CNCBuildCanvas(options);
```
--------------------------------
### Hex Color String Examples
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/types.md
Provides examples of valid hex color strings and their corresponding representations.
```javascript
"FFFFFF"
```
```javascript
"000000"
```
```javascript
"FF0000"
```
```javascript
"00FF00"
```
```javascript
"0000FF"
```
```javascript
"0088FF"
```
--------------------------------
### GCode Drawing Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/README.md
A practical example of defining GCode commands and then drawing them on the canvas, followed by zooming and updating the render.
```javascript
var gcode = "G90\n" + // Absolute mode
"G00X0Y0Z0.01\n" + // Move to origin
"G01Z-0.1\n" + // Move down
"X50Y50\n" + // Move to (50, 50)
"Z0.01\n"; // Move up
canvas.drawGCode(gcode);
canvas.model.zoomTo("front");
canvas.model.updateRender();
```
--------------------------------
### GCode Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
A sample GCode string demonstrating absolute positioning, rapid and linear moves, and spindle control.
```gcode
G90 ; Absolute mode
G00X0Y0Z0.01 ; Move to origin (rapid)
G01Z-0.1 ; Move down (cutting)
X10Y10 ; Move to 10,10
Z0.01 ; Move up
M05 ; Stop spindle
```
--------------------------------
### CadCanvas Initialization and Configuration
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/README.md
Example of initializing CadCanvas with various configuration options for display, colors, and platform settings.
```javascript
$("#canvasDiv").CNCBuildCanvas({
// Display
drawDimensions: true,
measurementUnit: "mm",
// Colors (hex without #)
toolEnabledColor: "FFFFFF",
toolDisabledColor: "999999",
platformColor: "0088FF",
dimensionColor: "0088FF",
// Platform
platform: {
grid: true,
size: [100, 100],
ruleIncrement: [10, 10]
},
// Toolhead
toolhead: {
enabled: true
}
});
```
--------------------------------
### GCode Structure Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Illustrates the typical structure of GCode commands, including comments and coordinate values.
```plaintext
G90 ; Set absolute mode
G00X0Y0 ; Move to origin
G01Z-0.1 ; Move down (cutting)
X10Y10 ; Move to 10,10 (no Z = stays at current Z)
Z0.01 ; Move up
M05 ; Stop spindle
```
--------------------------------
### Complete CNCBuildCanvas Workflow Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Demonstrates creating a canvas with specific configurations, loading GCode, setting the view, and previewing toolhead movement. Ensure the toolhead is enabled in the configuration for preview functionality.
```javascript
// Create canvas with configuration
var canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
grid: true,
size: [100, 100],
ruleIncrement: [10, 10]
},
drawDimensions: true,
toolhead: { enabled: true }
});
// Load gcode
var gcode = "G90\nG00X0Y0Z0.01\nG01Z-0.1\nX50Y50\nZ0.01\n";
canvas.drawGCode(gcode);
// Set view and render
canvas.model.zoomTo("front");
canvas.model.updateRender();
// Preview toolhead movement (if toolhead enabled)
canvas.toolhead.previewGCode();
```
--------------------------------
### Basic CadCanvas Setup and Rendering
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Demonstrates how to create a new CadCanvas instance, add basic geometry (lines along axes), and update the rendering with an isometric view.
```javascript
// Create and configure a CadCanvas
var canvas = new CadCanvas({
width: 800,
height: 600
});
// Add some geometry
canvas.model.addLine([[0, 0, 0], [10, 0, 0]], "FF0000"); // Red line on X-axis
canvas.model.addLine([[0, 0, 0], [0, 10, 0]], "00FF00"); // Green line on Y-axis
canvas.model.addLine([[0, 0, 0], [0, 0, 10]], "0000FF"); // Blue line on Z-axis
// Render and set isometric view
canvas.model.updateRender();
canvas.model.zoomTo("isometric");
```
--------------------------------
### GCode Example with Position Setting
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Demonstrates the use of G92 to set a new origin offset. Subsequent coordinates are relative to this new origin.
```plaintext
"G00X10Y10\nG92X0Y0\nG01X5Y5"
```
--------------------------------
### GCode Attributes Map Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/types.md
Illustrates the parsed GCode attributes map from a G01 command.
```javascript
{
x: "10.5",
y: "-5",
z: "0.1"
}
```
--------------------------------
### CadCanvas Initialization and Drawing
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Example of initializing the CNCBuildCanvas and drawing GCode, ensuring it's wrapped in a DOM ready handler.
```javascript
$(function() {
var canvas = $("#canvasDiv").CNCBuildCanvas();
canvas.drawGCode(gcode);
});
```
--------------------------------
### Animate Toolhead Preview
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Preview tool movement step-by-step after drawing GCode. The animation starts with a default delay of 100ms per line.
```javascript
// Preview tool movement step-by-step
canvas.drawGCode(gcode);
canvas.toolhead.previewGCode(); // Starts animation at 100ms per line
```
--------------------------------
### Start Mouse Operation
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Initiates a mouse operation such as rotation, movement, or zoom. Provide the operation type and initial mouse coordinates.
```javascript
model.mouse.start(operation, x, y)
```
```javascript
// Start rotation at mouse position 100, 200
model.mouse.start("rotation", -100, 200);
```
--------------------------------
### HTML Structure for CadCanvas Example
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
This HTML sets up the necessary container for the CadCanvas and provides buttons for previewing the toolhead and changing the view. Ensure the canvas container has an ID matching the one used in the JavaScript initialization.
```html
```
--------------------------------
### Get Bounds
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Shows how to retrieve the center coordinates and dimensions (size) of the current drawing on the canvas.
```javascript
var center = canvas.model.get_drawing_center();
var size = canvas.model.get_drawing_size();
```
--------------------------------
### Information Retrieval
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/README.md
Shows how to get the drawing center coordinates and dimensions from the model.
```javascript
model.get_drawing_center() // Get [x, y, z]
model.get_drawing_size() // Get [width, height, depth]
```
--------------------------------
### Example: Move Toolhead Indicator
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Moves the toolhead indicator to coordinates (10, 20, -0.1) and updates the canvas render. Ensure the toolhead is enabled and initialized.
```javascript
canvas.toolhead.moveTo(10, 20, -0.1);
canvas.model.updateRender();
```
--------------------------------
### Model Resize Event Binding
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Example of binding a 'resize' event listener to the CadCanvas model to detect changes in drawing boundaries.
```javascript
$(canvas.model).on("resize", function() {
console.log("Drawing boundaries updated");
console.log(canvas.model.get_drawing_size());
});
```
--------------------------------
### Configure CNC Router Simulation
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Configure the canvas for CNC router simulation. This setup enables dimensions, uses millimeters, and defines colors for cutting, rapid moves, and the platform.
```javascript
$("#canvasDiv").CNCBuildCanvas({
drawDimensions: true,
measurementUnit: "mm",
toolEnabledColor: "00FF00", // Green for cuts
toolDisabledColor: "666666", // Dark gray for rapids
platformColor: "333333", // Dark platform
platform: {
grid: true,
outline: true,
size: [600, 400],
ruleIncrement: [50, 50]
},
toolhead: {
enabled: true
}
});
```
--------------------------------
### CadCanvas Project File Structure
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Provides an overview of the CadCanvas project's directory structure, detailing the organization of source files, libraries, examples, build outputs, and utility scripts.
```plaintext
cadcanvas/
├── src/
│ ├── js/
│ │ ├── cad_canvas/
│ │ │ ├── cad_canvas.js (Main class, 109 lines)
│ │ │ ├── model.js (3D engine, 296 lines)
│ │ │ ├── mouse_interaction_model.js (Input, 123 lines)
│ │ │ └── ext/
│ │ │ ├── cnc_build_canvas.js (CNC extension, 171 lines)
│ │ │ ├── dimension.js (Measurements, 23 lines)
│ │ │ └── toolhead.js (Toolhead, 46 lines)
│ │ ├── gcode_interpretter.js (Parser, 142 lines)
│ │ └── orthographic_projection.js (Three.js ext, 27 lines)
│ └── css/
│ └── cad_canvas.css
├── lib/
│ ├── three/ (Three.js fork)
│ ├── jquery-1.4.2.min.js
│ └── jquery.mousewheel.min.js
├── examples/
│ ├── gcode_canvas.html (Main example)
│ └── gcode/ (GCode samples)
├── build/
│ └── cad_canvas.js (Compiled version)
└── utils/
└── closure.js (Build system)
```
--------------------------------
### Get Drawing Information
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Retrieve the drawing's center coordinates, size (width, height, depth), and bounding box. Useful for understanding the drawing's extent.
```javascript
var center = canvas.model.get_drawing_center();
var size = canvas.model.get_drawing_size();
console.log("Center:", center); // [centerX, centerY, centerZ]
console.log("Size:", size); // [width, height, depth]
console.log("Bounds:", {
x: [center[0] - size[0]/2, center[0] + size[0]/2],
y: [center[1] - size[1]/2, center[1] + size[1]/2],
z: [center[2] - size[2]/2, center[2] + size[2]/2]
});
```
--------------------------------
### Configure Small 3D Printer Platform
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Sets up a 200x200mm build platform with a 10mm grid and outline.
```javascript
platform: {
size: [200, 200],
ruleIncrement: [10, 10],
grid: true,
outline: true
}
```
--------------------------------
### Initialize and Draw GCode
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Demonstrates the common pattern of initializing a CNCBuildCanvas, drawing GCode, setting the view to front, and updating the render.
```javascript
var canvas = $("#canvasDiv").CNCBuildCanvas(options);
canvas.drawGCode(gcode);
canvas.model.zoomTo("front");
canvas.model.updateRender();
```
--------------------------------
### Get Drawing Size
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Returns the size [X, Y, Z] of the drawing's bounding box.
```javascript
model.get_drawing_size()
```
--------------------------------
### Configure 3D Printer Preview
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Set up the canvas for a 3D printer preview. This configuration enables dimensions, sets units to millimeters, and defines colors for tool extrusion and moves.
```javascript
$("#canvasDiv").CNCBuildCanvas({
drawDimensions: true,
measurementUnit: "mm",
endcapSize: 0.25,
toolEnabledColor: "FF6600", // Orange for extrusion
toolDisabledColor: "CCCCCC", // Light gray for moves
platform: {
grid: true,
outline: true,
size: [200, 200],
ruleIncrement: [20, 20]
}
});
```
--------------------------------
### Get Drawing Center Coordinates
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Returns the center coordinates [X, Y, Z] of the drawing's bounding box.
```javascript
model.get_drawing_center()
```
--------------------------------
### Color Format
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Specifies the color format as a hexadecimal string without the '#' prefix. Examples show how to represent red, green, and blue.
```javascript
"RRGGBB" // Hex without #
// Examples: "FF0000" (red), "00FF00" (green), "0000FF" (blue)
```
--------------------------------
### Initialize Canvas with Platform Configuration
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Initialize the CNCBuildCanvas with specific platform and display settings. Ensure the target div exists and has CSS size properties.
```javascript
var canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
grid: true,
size: [300, 300],
ruleIncrement: [30, 30]
},
drawDimensions: true,
toolhead: { enabled: true }
});
```
--------------------------------
### mouse.start
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Initiates a mouse operation, such as rotation, movement, or zoom, by setting the initial mouse position.
```APIDOC
## mouse.start(operation, x, y)
### Description
Initiates a mouse operation.
### Method
model.mouse.start(operation, x, y)
### Parameters
#### Path Parameters
- **operation** (string) - Required - Operation type: "rotation", "movement", "zoom"
- **x** (number) - Required - Initial mouse X position
- **y** (number) - Required - Initial mouse Y position
### Request Example
```javascript
// Start rotation at mouse position 100, 200
model.mouse.start("rotation", -100, 200);
```
```
--------------------------------
### Configure Platform with No Grid
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Sets up a 300x300mm platform showing only the outline, without a grid.
```javascript
platform: {
size: [300, 300],
grid: false,
outline: true
}
```
--------------------------------
### Styling the CadCanvas Element
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Applies CSS styles to the canvas element using the `cad_canvas` class. This example sets a border and background color.
```css
.cad_canvas {
border: 1px solid #ccc;
background-color: #000;
}
```
--------------------------------
### Initialize CadCanvas with Options
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Instantiate the base CadCanvas class with custom width and height settings.
```javascript
var canvas = new CadCanvas({
width: 1024,
height: 768
});
```
--------------------------------
### Initialize CNCBuildCanvas with Partial Configuration
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Override specific CNCBuildCanvas options, such as platform size and grid display, while allowing other settings to use their default values. This is useful for minor adjustments.
```javascript
// Only override platform size and add grid
$("#canvasDiv").CNCBuildCanvas({
platform: {
size: [200, 200],
grid: true
}
});
// All other options use defaults
```
--------------------------------
### CadCanvas.MouseInteractionModel Class Definition
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Details the CadCanvas.MouseInteractionModel, a mixin for handling mouse events within CadCanvas.Model. It provides methods for starting, stopping, updating position, and scrolling.
```javascript
// Mouse event handling mixin (injected into CadCanvas.Model).
// Key Methods:
// - mouse.start() — Begin operation
// - mouse.stop() — End operation
// - mouse.updatePosition() — Handle movement
// - mouse.scroll() — Handle wheel
```
--------------------------------
### Initialize and Draw GCode on CadCanvas
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
This snippet demonstrates how to initialize the CNCBuildCanvas with specific configurations, draw GCode, set the view, and update the rendering. It also includes handlers for previewing the toolhead and changing the view.
```javascript
$(function() {
// Create canvas with configuration
var canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
grid: true,
size: [100, 100],
ruleIncrement: [10, 10]
},
drawDimensions: true,
toolhead: { enabled: true }
});
// GCode to render
var gcode = "G90\n" +
"G00X0Y0Z0.01\n" +
"G01Z-0.1\n" +
"X50Y50\n" +
"Z0.01\n" +
"G00X0Y0\n";
// Draw on canvas
canvas.drawGCode(gcode);
// Set view and render
canvas.model.zoomTo("isometric");
canvas.model.updateRender();
// Button handler
$("#previewBtn").click(function() {
canvas.toolhead.previewGCode();
});
// Handle view changes
$(".view-btn").click(function() {
var view = $(this).data("view");
canvas.model.zoomTo(view);
canvas.model.updateRender();
});
});
```
--------------------------------
### GCode Parsing and Rendering
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/README.md
Demonstrates how to parse and render GCode strings or interpret GCode data with a callback.
```javascript
canvas.drawGCode(gcode_string) // Parse and render
GCode.interpret(gcode, callback) // Parse only
```
--------------------------------
### Minimal Configuration
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
A minimal configuration to display only the gcode, disabling the platform and dimensions. This is useful for focusing solely on the toolpath.
```javascript
// Show gcode only, no platform or dimensions
$("#canvasDiv").CNCBuildCanvas({
drawDimensions: false,
platform: {
grid: false,
outline: false
},
toolhead: {
enabled: false
}
});
```
--------------------------------
### Initialize CNCBuildCanvas with Complete Options
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Configure a CNC-specific build canvas with all available display, color, platform, and toolhead options. Ensure the target element exists on the page.
```javascript
var options = {
// Display configuration
drawDimensions: true,
measurementUnit: "mm",
endcapSize: 0.25,
// Colors (hex without # prefix)
toolEnabledColor: "FFFFFF", // White
toolDisabledColor: "999999", // Gray
toolHistoryColor: "0033CC", // Blue
platformColor: "0088FF", // Light blue
dimensionColor: "0088FF", // Light blue
// Platform configuration
platform: {
grid: true,
outline: true,
size: [300, 300], // 300mm x 300mm
ruleIncrement: [25, 25] // 25mm grid
},
// Toolhead
toolhead: {
enabled: true
}
};
$("#canvasDiv").CNCBuildCanvas(options);
```
--------------------------------
### Include Dependencies
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Include the necessary JavaScript files for CadCanvas, Three.js, jQuery, and the mousewheel plugin.
```html
```
--------------------------------
### CNCBuildCanvas Options
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Configuration options for initializing a CNCBuildCanvas instance. Includes settings for measurements, colors, platform, and toolhead.
```javascript
{
drawDimensions: true,
measurementUnit: "mm",
endcapSize: 0.25,
toolEnabledColor: "FFFFFF",
toolDisabledColor: "999999",
toolHistoryColor: "0033CC",
platformColor: "0088FF",
dimensionColor: "0088FF",
platform: {
grid: false,
outline: true,
size: [10, 10],
ruleIncrement: [0.5, 0.5]
},
toolhead: {
enabled: true
}
}
```
--------------------------------
### Add Line to Scene
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Adds a line segment to the 3D scene. Specify the start and end points, color, line width, opacity, and whether it should be included in boundary calculations. Remember to call `updateRender()` after adding geometry.
```javascript
// Draw a red line from origin to 10,10,0
var line = model.addLine(
[[0, 0, 0], [10, 10, 0]],
"FF0000", // red
2, // lineWidth
1, // full opacity
false // include in bounds
);
model.updateRender();
```
--------------------------------
### Toolhead Constructor
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Initializes the toolhead indicator for GCode preview. Requires a reference to the parent `CNCBuildCanvas` instance.
```javascript
CNCBuildCanvasToolhead(buildCanvas)
```
--------------------------------
### Initialize CadCanvas
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Initialize CadCanvas with basic settings or custom configurations. Load and display GCode after initialization.
```javascript
$(function() {
// Basic initialization
var canvas = $("#canvasDiv").CNCBuildCanvas();
// Or with configuration
var canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
grid: true,
size: [100, 100],
ruleIncrement: [10, 10]
},
drawDimensions: true,
measurementUnit: "mm"
});
// Load and display GCode
canvas.drawGCode(gcode_string);
canvas.model.updateRender();
});
```
--------------------------------
### previewGCode()
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Animates the toolhead movement through all gcode lines in sequence. This method resets the preview line, moves the toolhead to each gcode line position at 100ms intervals, and automatically stops at the end of the gcode lines. It uses `window.setInterval` internally.
```APIDOC
## previewGCode()
### Description
Animates toolhead movement through all gcode lines in sequence.
### Method
```javascript
toolhead.previewGCode()
```
### Parameters
This method does not accept any parameters.
### Request Example
```javascript
// Start the toolhead animation
canvas.toolhead.previewGCode();
```
### Response
This method does not return a value, but it triggers a visual animation of the toolhead on the canvas.
```
--------------------------------
### Tool Enable/Disable Commands (M03/M05)
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Shows the usage of M03 (spindle on) and M05 (spindle off) commands. Note: The current implementation does not track `toolEnabled` state for these commands.
```javascript
var gcode = "G90\n" +
"M03\n" + // Spindle on (note: not tracked)
"G01X10\n" +
"M05\n" + // Spindle off (note: not tracked)
"G01X20\n";
GCode.interpret(gcode, function(lines, toolEnabled) {
// Both lines have toolEnabled = true (limitation of current implementation)
console.log(toolEnabled);
});
```
--------------------------------
### Closure Compiler Build Command
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Command to compile and minify all JavaScript source files using Google Closure Compiler.
```bash
java -jar compiler.jar \
--js src/js/**/*.js \
--js_output_file build/cad_canvas.js
```
--------------------------------
### Platform Configuration Object
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/types.md
Defines the visual and dimensional properties of the platform, including grid and outline settings. Grid lines are centered and extend from -size/2 to +size/2.
```javascript
{
grid: boolean, // Display grid pattern
outline: boolean, // Display boundary lines
size: [number, number], // Platform dimensions [width, length]
ruleIncrement: [number, number] // Grid spacing [x_spacing, y_spacing]
}
```
```javascript
{
grid: true,
outline: true,
size: [200, 150], // 200 units wide, 150 units long
ruleIncrement: [10, 10] // 10-unit grid spacing
}
```
--------------------------------
### Preset and Manual Navigation
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Control the camera view using preset orientations or manual rotation and zoom. Use 'model.zoomTo()' for presets and 'model.rotate()' or 'model.setZoomDistance()' for manual control.
```javascript
// Preset views
model.zoomTo("front"); // Front view
model.zoomTo("top"); // Top-down
model.zoomTo("isometric"); // 3D isometric
model.zoomTo("left"); // Left side
// Manual rotation (in radians)
model.rotate(0, 0, 0); // Reset
model.rotate(Math.PI/4, 0, 0); // 45° around X
model.rotate(null, Math.PI/2, 0); // 90° around Y (keep X)
// Zoom
model.setZoomDistance(1);
model.incrementZoom(velocity); // Non-linear zoom
model.resetZoom(); // Fit view
// Pan
model.move(x, y); // Move center to x, y
model.center(); // Center on drawing
```
--------------------------------
### CNCBuildCanvas Configuration Options
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Configure display, colors, platform, and toolhead settings for CNCBuildCanvas. Colors are specified as hex strings without '#'.
```javascript
{
// Display
drawDimensions: true,
measurementUnit: "mm",
endcapSize: 0.25,
// Colors (hex without #)
toolEnabledColor: "FFFFFF", // Active tool
toolDisabledColor: "999999", // Inactive
toolHistoryColor: "0033CC", // History
platformColor: "0088FF", // Platform
dimensionColor: "0088FF", // Dimensions
// Platform
platform: {
grid: true, // Show grid
outline: true, // Show boundary
size: [100, 100], // Width x length
ruleIncrement: [10, 10] // Grid spacing
},
// Toolhead
toolhead: {
enabled: true
}
}
```
--------------------------------
### Initialize CADCanvas and Draw G-code
Source: https://github.com/d1plo1d/cadcanvas/blob/master/examples/gcode_canvas.html
Initializes the CADCanvas with specified platform settings and then draws G-code onto the canvas. Ensure the canvas element and gcode variable are available.
```javascript
var canvas;
$(function() {
canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
// size: [400, 400],
size: [10, 10],
ruleIncrement: [0.5, 0.5],
}
});
// canvas.model.zoom_scale = 100;
canvas.model.zoom_scale = 1;
//$("#canvasDiv").html(gcode);
canvas.drawGCode(gcode);
canvas.model.updateRender();
});
```
--------------------------------
### Initialize CNCBuildCanvas jQuery Extension
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Initialize the CNCBuildCanvas jQuery extension with various configuration options for platform, dimensions, and toolhead.
```javascript
$("#canvasDiv").CNCBuildCanvas({
drawDimensions: true,
measurementUnit: "mm",
platform: {
grid: true,
size: [200, 200],
ruleIncrement: [20, 20]
},
toolhead: { enabled: true }
});
```
--------------------------------
### CadCanvas.Model Constructor
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Initializes the 3D scene with camera, scene, renderer, and mouse interaction model. Sets initial front view.
```APIDOC
## CadCanvas.Model Constructor
### Description
Initializes the 3D scene with camera, scene, renderer, and mouse interaction model. Sets initial front view.
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Parameters
- **width** (number) - Required - Canvas width in pixels
- **height** (number) - Required - Canvas height in pixels
### Return
`CadCanvas.Model` — The model instance.
```
--------------------------------
### Color Conversion to Integer
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/types.md
Demonstrates the conversion of a hex color string to an integer for Three.js materials.
```javascript
var materialColor = parseInt(hex_color, 16);
// "FF0000" → 16711680
```
--------------------------------
### Configure Large CNC Machine Platform
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Sets up a 1000x600mm work area with a 100mm grid and outline.
```javascript
platform: {
size: [1000, 600],
ruleIncrement: [100, 100],
grid: true,
outline: true
}
```
--------------------------------
### Simulating Hardware Endstops
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Checks if movement would go below zero on any axis and resets it to the offset. This simulates hardware endstops at the origin.
```javascript
if (cnc_data[axis] - cnc_data_offset[axis] < 0) {
cnc_data[axis] = cnc_data_offset[axis];
}
```
--------------------------------
### Drawing Boundary Optimization
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Illustrates the drawing boundary optimization technique using a `drawing_dirty` flag. Calculations are deferred until explicitly needed.
```javascript
this.drawing_dirty = false; // Start clean
// When adding a line
if (vec > maxima[i]) {
drawing_maxima[i] = vec;
drawing_dirty = true; // Mark for recalculation
}
// Lazy calculation
get_drawing_center() {
this.update_drawing(); // Only recalculates if dirty
return this.drawing_center;
}
```
--------------------------------
### Create ColorFillMaterial for Text and Particles
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Instantiates a Three.js material for rendering text and particles with specified color and opacity.
```javascript
new THREE.ColorFillMaterial(hex, opacity)
```
--------------------------------
### Apply Custom Color Scheme
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/configuration.md
Initializes CNCBuildCanvas with a custom color scheme for a dark theme.
```javascript
// Dark theme
$("#canvasDiv").CNCBuildCanvas({
platformColor: "333333",
dimensionColor: "FFFF00",
toolEnabledColor: "00FF00",
toolDisabledColor: "666666"
});
```
--------------------------------
### Setting Current Position with G92
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Demonstrates using the G92 command to set the current position as the new origin, affecting subsequent relative movements.
```javascript
var gcode = "G00X10Y10\n" + // Move to (10, 10)
"G92X0Y0\n" + // Set current position as origin
"G01X5Y5\n"; // Move to (5, 5) relative to new origin
GCode.interpret(gcode, function(lines, toolEnabled) {
console.log(lines[0], "→", lines[1]);
});
// Output:
// [10, 10, 0] → [5, 5, 0]
```
--------------------------------
### $.fn.CNCBuildCanvas
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Initializes the CNCBuildCanvas jQuery extension on a selected element, allowing for the creation of a CNC machine build platform with G-code preview and toolhead visualization. It accepts an optional configuration object to customize its appearance and behavior.
```APIDOC
## $.fn.CNCBuildCanvas
### Description
Initializes a CNCBuildCanvas on the selected jQuery element.
### Method
POST
### Endpoint
/jquery-extension
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **options** (Object) - Optional - Configuration object for CNCBuildCanvas.
### Request Example
```javascript
// Basic initialization
var canvas = $("#canvasDiv").CNCBuildCanvas();
// With configuration
var canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
grid: true,
outline: true,
size: [200, 200],
ruleIncrement: [10, 10]
},
drawDimensions: true,
measurementUnit: "mm"
});
```
### Response
#### Success Response (200)
- **instance** (Object) - Returns the CNCBuildCanvas instance with jQuery chainability.
#### Response Example
```json
{
"instance": "CNCBuildCanvas Instance"
}
```
```
--------------------------------
### CNCBuildCanvasToolhead.moveTo()
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Moves the toolhead indicator to a specified position in world space. This method is used for previewing toolhead movement.
```APIDOC
## moveTo(x, y, z, speed)
### Description
Moves the toolhead indicator to a position. This method is used for previewing toolhead movement.
### Method
`toolhead.moveTo(x, y, z, speed)`
### Parameters
- **x** (number) - Required - X coordinate in world space.
- **y** (number) - Required - Y coordinate in world space.
- **z** (number) - Required - Z coordinate in world space.
- **speed** (number) - Optional - Animation speed (not currently used).
**Note:** Position is divided by 2 due to a workaround for a Three.js issue.
### Request Example
```javascript
canvas.toolhead.moveTo(10, 20, -0.1);
canvas.model.updateRender();
```
### Response
(No explicit response defined, modifies toolhead indicator position)
```
--------------------------------
### CadCanvas Options
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Configuration options for initializing a base CadCanvas instance. Sets the canvas dimensions.
```javascript
{
width: 600,
// Canvas width (px)
height: 400
// Canvas height (px)
}
```
--------------------------------
### Initialize CNCBuildCanvas
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Initializes the CNCBuildCanvas on a selected jQuery element. The 'options' parameter is an object for configuration.
```javascript
var canvas = $("#canvasDiv").CNCBuildCanvas();
```
```javascript
var canvas = $("#canvasDiv").CNCBuildCanvas({
platform: {
grid: true,
outline: true,
size: [200, 200],
ruleIncrement: [10, 10]
},
drawDimensions: true,
measurementUnit: "mm"
});
```
--------------------------------
### Project Directory Structure
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/MANIFEST.md
This snippet shows the directory structure of the CadCanvas project's output documentation. It highlights the main markdown files and the API reference subdirectory.
```text
output/
├── README.md (Start here)
├── QUICK-START.md (Getting started)
├── INDEX.md (Find topics)
├── types.md (Type reference)
├── configuration.md (Options)
├── errors.md (Issues)
├── MANIFEST.md (This file)
└── api-reference/
├── cadcanvas-core.md
├── cnc-build-canvas.md
├── gcode-interpreter.md
└── utilities.md
```
--------------------------------
### Basic GCode Interpretation (Absolute Mode)
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Interprets a GCode string in absolute mode, demonstrating movement commands and callback logging.
```javascript
var gcode = "G90\n" + // Absolute mode
"G00X0Y0Z0.01\n" + // Move to origin
"G01Z-0.1\n" + // Move down
"X10Y10\n" + // Move to (10, 10)
"Z0.01\n"; // Move up
GCode.interpret(gcode, function(lines, toolEnabled) {
console.log(lines[0], "→", lines[1]);
});
// Output:
// [0, 0, 0.01] → [10, 10, -0.1]
// [10, 10, -0.1] → [10, 10, 0.01]
```
--------------------------------
### Preview G-code Animation
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Animates the toolhead movement through all gcode lines in sequence. Resets previewLine to 0, moves the toolhead at 100ms intervals, and stops automatically at the end. Uses window.setInterval internally.
```javascript
toolhead.previewGCode()
```
```javascript
// Start the toolhead animation
canvas.toolhead.previewGCode();
```
--------------------------------
### Change View
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Illustrates how to change the canvas view to an isometric perspective and then update the rendering to reflect the change.
```javascript
canvas.model.zoomTo("isometric");
canvas.model.updateRender();
```
--------------------------------
### Draw GCode on Canvas
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Loads and displays gcode preview on the canvas. The method parses gcode, draws tool paths, centers the view, and updates the renderer.
```javascript
var gcode = "G90\n" +
"G00X0Y0Z0.01\n" +
"G01Z-0.1\n" +
"X10Y10\n" +
"Z0.01\n";
canvas.drawGCode(gcode);
canvas.model.updateRender();
```
--------------------------------
### CadCanvas Core Components Architecture
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/INDEX.md
Illustrates the main CadCanvas controller and its relationship with the 3D engine and mouse interaction models.
```plaintext
CadCanvas (Main UI Controller)
└── CadCanvas.Model (3D Engine)
├── THREE.Camera
├── THREE.Scene
├── THREE.CanvasRenderer
└── CadCanvas.MouseInteractionModel
├── mouse.ops (operation states)
└── mouse methods (start, stop, updatePosition, scroll)
```
--------------------------------
### Instantiate CadCanvas
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Create a new CadCanvas instance with specified dimensions. Access its model for further manipulation.
```javascript
var canvas = new CadCanvas({
width: 800,
height: 600
});
// Access underlying model
canvas.model.zoomTo("isometric");
canvas.model.addLine([[0,0,0], [10,0,0]], "FF0000");
canvas.model.updateRender();
```
--------------------------------
### Switch Canvas Views
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/QUICK-START.md
Change the camera view to presets like 'isometric' or 'front'. You can also apply rotation limits before switching views.
```javascript
// Isometric view
canvas.model.zoomTo("isometric");
canvas.model.updateRender();
// Front view with rotation constraint
canvas.model.rotation_limits = [[-Math.PI/2, 0], null, null];
canvas.model.zoomTo("front");
canvas.model.updateRender();
```
--------------------------------
### drawGCode(gcode_string)
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cnc-build-canvas.md
Loads and displays a G-code preview on the canvas. This method parses the provided G-code string, draws the toolpath with specified colors, centers the view, and updates the renderer.
```APIDOC
## drawGCode(gcode_string)
### Description
Loads and displays gcode preview on the canvas.
### Method
POST
### Endpoint
/gcode
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
- **gcode_string** (string) - Required - GCode text with commands separated by newlines.
### Request Example
```javascript
var gcode = "G90\n" +
"G00X0Y0Z0.01\n" +
"G01Z-0.1\n" +
"X10Y10\n" +
"Z0.01\n";
canvas.drawGCode(gcode);
canvas.model.updateRender();
```
### Response
#### Success Response (200)
- **status** (string) - Indicates the success of the operation.
#### Response Example
```json
{
"status": "GCode preview drawn successfully"
}
```
```
--------------------------------
### move(x, y)
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Pans the view to center on the specified world coordinates (x, y).
```APIDOC
## move(x, y)
### Description
Pans the view to center on world coordinates (x, y).
### Method
```javascript
model.move(x, y)
```
### Parameters
#### Path Parameters
- **x** (number) - Required - X-coordinate in world space
- **y** (number) - Required - Y-coordinate in world space
```
--------------------------------
### Profile Rendering Speed in JavaScript
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Measures and logs Frames Per Second (FPS) by tracking time and frame count within the rendering loop.
```javascript
var lastTime = Date.now();
var frameCount = 0;
// In animation loop or after updateRender()
frameCount++;
var now = Date.now();
if (now - lastTime > 1000) {
console.log("FPS:", frameCount);
frameCount = 0;
lastTime = now;
}
```
--------------------------------
### Create Three.js Text Object
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Instantiates a Three.js Text object for 3D text rendering, requiring text content and a ColorFillMaterial.
```javascript
new THREE.Text(text, material)
```
--------------------------------
### Auto-Scale Platform to Fit GCode Bounds
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/errors.md
Retrieves the drawing size of the GCode and adjusts the platform size to fit these bounds, including a margin. This ensures the GCode appears centered and within the platform.
```javascript
var bounds = canvas.model.get_drawing_size();
// Adjust platform size to fit bounds with margin
```
--------------------------------
### CNCBuildCanvas Options Object
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/types.md
Configuration object for the $.fn.CNCBuildCanvas() method, controlling display, colors, platform, and toolhead settings.
```javascript
{
// Display configuration
drawDimensions: boolean, // Show measurements (default: true)
measurementUnit: string, // Unit label (default: "mm")
endcapSize: number, // Dimension endcap size (default: 0.25)
// Colors (hex strings)
toolEnabledColor: string, // Active tool color (default: "FFFFFF")
toolDisabledColor: string, // Inactive tool color (default: "999999")
toolHistoryColor: string, // Tool history color (default: "0033CC")
platformColor: string, // Platform color (default: "0088FF")
dimensionColor: string, // Dimension color (default: "0088FF")
// Platform configuration
platform: {
grid: boolean, // Show grid (default: false)
outline: boolean, // Show outline (default: true)
size: [number, number], // [width, length] (default: [10, 10])
ruleIncrement: [number, number] // Grid spacing (default: [0.5, 0.5])
},
// Toolhead configuration
toolhead: {
enabled: boolean // Show toolhead (default: true)
}
}
```
```javascript
{
drawDimensions: true,
measurementUnit: "mm",
endcapSize: 0.25,
toolEnabledColor: "FFFFFF",
toolDisabledColor: "999999",
toolHistoryColor: "0033CC",
platformColor: "0088FF",
dimensionColor: "0088FF",
platform: {
grid: true,
outline: true,
size: [100, 100],
ruleIncrement: [10, 10]
},
toolhead: {
enabled: true
}
}
```
--------------------------------
### ColorFillMaterial Properties
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/types.md
Lists the properties for the ColorFillMaterial, including color and opacity.
```javascript
{
hex: Color as integer,
opacity: Transparency 0-1
}
```
--------------------------------
### Apply Initial Z-Rotation Hack
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/errors.md
Applies a minimal Z-axis rotation to the initial camera orientation. This is a workaround for a rendering bug that can cause certain lines to disappear.
```javascript
this.rotation = [0, 0, 0.0001]; // Non-zero Z prevents some lines from disappearing
```
--------------------------------
### Validate Configuration Values
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/errors.md
Ensure configuration values for platform size are valid arrays of numbers before passing them to CadCanvas.
```javascript
function validateConfig(opts) {
if (!Array.isArray(opts.platform.size) || opts.platform.size.length !== 2) {
throw new Error("platform.size must be [width, length]");
}
if (typeof opts.platform.size[0] !== 'number') {
throw new Error("platform.size values must be numbers");
}
}
```
--------------------------------
### Create ColorStrokeMaterial for Lines
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/utilities.md
Instantiates a Three.js material specifically for rendering lines with customizable width, color, and opacity.
```javascript
new THREE.ColorStrokeMaterial(lineWidth, hex, opacity)
```
```javascript
var material = new THREE.ColorStrokeMaterial(2, 0xFF0000, 1);
// Thickness 2, red color, full opacity
```
--------------------------------
### Integrating GCode Interpretation with Canvas Drawing
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/gcode-interpreter.md
Illustrates how the `drawGCode` method uses `GCode.interpret` to process GCode and render lines on a canvas.
```javascript
canvas.drawGCode = function(gcode) {
GCode.interpret(gcode, $.proxy(function(line, toolEnabled) {
this.gcodeLines.push(line);
this.model.addLine(
line,
this.options[(toolEnabled ? "toolEnabledColor" : "toolDisabledColor")],
1
);
}, this));
this.model.center();
this.model.updateRender();
}
```
--------------------------------
### zoomTo(location)
Source: https://github.com/d1plo1d/cadcanvas/blob/master/_autodocs/api-reference/cadcanvas-core.md
Zooms to a preset viewpoint and centers the drawing. Accepts predefined location strings.
```APIDOC
## zoomTo(location)
### Description
Zooms to a preset viewpoint and centers the drawing.
### Method
```javascript
model.zoomTo(location)
```
### Parameters
#### Path Parameters
- **location** (string) - Required - Preset location: "front", "back", "left", "right", "top", "bottom", "front-rotated-left", "front-rotated-right", "isometric"
### Request Example
```javascript
// Switch to isometric view
model.zoomTo("isometric");
```
```