log.console("test_line2d starting") var time = use('time') var core = use('core') var line2d = use('line2d') var compositor = use('compositor') var film2d = use('film2d') var math = use('math/radians') var lines = [] var t = 0 var camera = { pos: {x: 400, y: 300}, width: 800, height: 600, anchor: {x: 0.5, y: 0.5} } var compositor_config = { clear: {r: 0.1, g: 0.1, b: 0.15, a: 1}, planes: [ { name: 'main', plane: 'main', camera: camera, resolution: {width: 800, height: 600}, presentation: 'stretch', clear: {r: 0.1, g: 0.1, b: 0.15, a: 1} } ] } function init() { // Row 1: Basic lines with different widths // Thin line lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 50, y: 550}, {x: 150, y: 520}, {x: 200, y: 560}], width: 4, tint: {r: 1, g: 0.3, b: 0.3, a: 1} }) // Medium line lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 250, y: 550}, {x: 350, y: 520}, {x: 400, y: 560}], width: 12, tint: {r: 0.3, g: 1, b: 0.3, a: 1} }) // Thick line lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 450, y: 550}, {x: 550, y: 520}, {x: 600, y: 560}], width: 24, tint: {r: 0.3, g: 0.3, b: 1, a: 1} }) // Variable width line lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 650, y: 560}, {x: 700, y: 520}, {x: 750, y: 560}], widths: [8, 24, 8], tint: {r: 1, g: 1, b: 0.3, a: 1} }) // Row 2: Textured ropes with different UV modes // Rope with repeat UV (default for ropes) lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 50, y: 450}, {x: 120, y: 420}, {x: 200, y: 440}, {x: 280, y: 400}], width: 20, image: 'examples/rope', uv: { mode: 'repeat', u_per_unit: 1 / 32 } }) // Rope with stretch UV lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 320, y: 450}, {x: 400, y: 420}, {x: 480, y: 440}, {x: 560, y: 400}], width: 20, image: 'examples/rope', uv: { mode: 'stretch' } }) // Rope with per-segment UV lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 600, y: 450}, {x: 660, y: 420}, {x: 720, y: 440}, {x: 780, y: 400}], width: 20, image: 'examples/rope', uv: { mode: 'per_segment' } }) // Row 3: Cap styles // Butt cap (default) lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 80, y: 320}, {x: 180, y: 320}], width: 20, cap: 'butt', tint: {r: 0.8, g: 0.5, b: 0.2, a: 1} }) // Square cap lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 250, y: 320}, {x: 350, y: 320}], width: 20, cap: 'square', tint: {r: 0.5, g: 0.8, b: 0.2, a: 1} }) // Round cap lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 420, y: 320}, {x: 520, y: 320}], width: 20, cap: 'round', tint: {r: 0.2, g: 0.5, b: 0.8, a: 1} }) // Round cap with texture lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 590, y: 320}, {x: 750, y: 320}], width: 24, cap: 'round', image: 'examples/rope' }) // Row 4: Complex paths // Wavy line (will be animated) var wave_points = [] var i = 0 for (i = 0; i < 12; i++) { wave_points[] = { x: 50 + i * 30, y: 220 + math.sine(i * 0.8) * 30 } } lines[] = line2d.polyline({ plane: 'main', layer: 0, points: wave_points, width: 14, image: 'examples/rope', uv: { mode: 'repeat', u_per_unit: 1 / 24 } }) // Spiral/coil var spiral_points = [] var angle = null var radius = null for (i = 0; i < 20; i++) { angle = i * 0.5 radius = 20 + i * 3 spiral_points[] = { x: 550 + math.cosine(angle) * radius, y: 200 + math.sine(angle) * radius } } lines[] = line2d.polyline({ plane: 'main', layer: 0, points: spiral_points, width: 10, image: 'examples/rope', uv: { mode: 'repeat', u_per_unit: 1 / 20 } }) // Row 5: Closed shapes and special cases // Closed triangle lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 80, y: 80}, {x: 140, y: 140}, {x: 20, y: 140}], width: 8, closed: true, tint: {r: 1, g: 0.5, b: 0.8, a: 1} }) // Closed pentagon var pent_points = [] for (i = 0; i < 5; i++) { angle = i * (2 * 3.14159 / 5) - 3.14159 / 2 pent_points[] = { x: 220 + math.cosine(angle) * 50, y: 110 + math.sine(angle) * 50 } } lines[] = line2d.polyline({ plane: 'main', layer: 0, points: pent_points, width: 10, closed: true, image: 'examples/rope', uv: { mode: 'repeat', u_per_unit: 1 / 30 } }) // Animated rope (will swing) lines[] = line2d.polyline({ plane: 'main', layer: 1, points: [{x: 400, y: 50}, {x: 400, y: 100}, {x: 400, y: 150}], width: 16, image: 'examples/rope', uv: { mode: 'repeat', u_per_unit: 1 / 20 } }) // Fuse/wire with opacity gradient (simulated via tint) lines[] = line2d.polyline({ plane: 'main', layer: 0, points: [{x: 500, y: 80}, {x: 580, y: 60}, {x: 660, y: 100}, {x: 740, y: 70}], width: 8, image: 'examples/rope', opacity: 0.7, tint: {r: 1, g: 0.8, b: 0.4, a: 1} }) log.console("test_line2d initialized with " + text(length(lines)) + " lines") } function update(dt) { t += dt // Animate the wavy line (index 11) var pts = null var i = 0 if (lines[11]) { pts = lines[11].points for (i = 0; i < length(pts); i++) { pts[i].y = 220 + math.sine(i * 0.8 + t * 2) * 30 } lines[11].set_points(pts) } // Animate the hanging rope (index 15) - swing like a pendulum var swing = null if (lines[15]) { swing = math.sine(t * 1.5) * 40 pts = [ {x: 400, y: 50}, {x: 400 + swing * 0.5, y: 100}, {x: 400 + swing, y: 150} ] lines[15].set_points(pts) } // Animate UV offset on the spiral (index 12) for scrolling texture effect if (lines[12]) { lines[12].uv.u_offset = t * 0.5 lines[12]._rebuild() } // Pulse opacity on the fuse (index 16) if (lines[16]) { lines[16].opacity = 0.5 + 0.5 * math.sine(t * 3) } } function render() { var plan = compositor.compile(compositor_config) return compositor.execute(plan) } init() core.start({ width: 800, height: 600, title: "Test Line2D - Ropes and Lines", framerate: 60, update: update, render: render })