i have three.js code for a 2d charting application My requirements: have a line where…
- User
i have three.js code for a 2d charting application
My requirements: have a line where the user can click and drag anywhere on the line. the 'endpoint' of the line for the given movement should be considered whichever is closest to the mouse cursor the end of the line should not jump to the mouse cursor but rather the movement of the lines endpoint should exactly mirror the movement of the cursor
Currently the code renders candlesticks. Please implement the additional functionality
import * as THREE from './three.module.js';
// Scene setup const scene = new THREE.Scene(); scene.background = new THREE.Color(0x000000);
// Camera setup const camera = new THREE.OrthographicCamera( window.innerWidth / -200, window.innerWidth / 200, window.innerHeight / 200, window.innerHeight / -200, 1, 1000 ); camera.position.set(0, 0, 500);
// Renderer setup const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
// Function to generate random OHLC data function generateOHLCData(count) { const data = []; let open = 50; // Starting price for (let i = 0; i < count; i++) { let high = open + Math.random() * 10; let low = open - Math.random() * 10; let close = low + Math.random() * (high - low); data.push({ open, high, low, close }); open = close; // Next open starts where the last close was } return data; }
// Convert RGB to normalized color values (0 to 1) function rgbToNormalized(r, g, b) { return [r / 255, g / 255, b / 255]; }
// Create instanced candlesticks with uniform borders and wicks function createInstancedCandlesticks(data) { const barWidth = 1; const maxCandlesticks = data.length;
// Materials const material = new THREE.MeshBasicMaterial({ vertexColors: true });
// Geometries for the candlestick body and borders const geometryBar = new THREE.BoxGeometry(barWidth, 1, 0);
// Create instanced mesh for candlesticks const instancedMesh = new THREE.InstancedMesh(geometryBar, material, maxCandlesticks);
const dummy = new THREE.Object3D(); const wickGeometry = new THREE.BufferGeometry(); const wickVertices = new Float32Array(maxCandlesticks * 6); // 2 points for each wick const wickColors = new Float32Array(maxCandlesticks * 6); // Color for each wick point
// Define colors in normalized RGB const bullishColor = rgbToNormalized(0, 70, 0); // Custom RGB for bullish wicks (green) const bearishColor = rgbToNormalized(138, 0, 0); // Custom RGB for bearish wicks (red) const colorArray = new Float32Array(maxCandlesticks * 3); // RGB for each instance
let currentX = 0;
data.forEach((d, index) => { const isBullish = d.close >= d.open; const height = Math.abs(d.close - d.open); const y = (d.close + d.open) / 2;
// Set the position and scale for the body dummy.position.set(currentX, y, 0); dummy.scale.set(barWidth, height, 1); dummy.updateMatrix(); instancedMesh.setMatrixAt(index, dummy.matrix); // Set color for each instance if (isBullish) { colorArray.set(bullishColor, index * 3); // Set bullish color wickColors.set([...bullishColor, ...bullishColor], index * 6); // Set wick color (two points) } else { colorArray.set(bearishColor, index * 3); // Set bearish color wickColors.set([...bearishColor, ...bearishColor], index * 6); // Set wick color (two points) } // Wick positions wickVertices.set([ currentX, d.low, -0.5, // Bottom of the wick currentX, d.high, -0.5 // Top of the wick ], index * 6); // Increment x for the next candlestick currentX += barWidth * 1.5; - Assistant