test commit

master
Jake 2025-11-03 11:04:53 +08:00
parent c1c7b41135
commit 7fbc41f8b7
2 changed files with 30 additions and 20 deletions

View File

@ -5,7 +5,7 @@ export class CurveEditor {
canvas.height = canvas.offsetHeight; canvas.height = canvas.offsetHeight;
this.ctx = canvas.getContext('2d'); this.ctx = canvas.getContext('2d');
this.timelineLength = timelineLength; this.timelineLength = timelineLength;
this.logicalHeight = 800;
this.scale = 1; this.scale = 1;
this.scaleX = 1; this.scaleX = 1;
@ -101,10 +101,11 @@ export class CurveEditor {
view.setUint16(offset, seg.endHandleX, true); offset += 2; view.setUint16(offset, seg.endHandleX, true); offset += 2;
view.setInt16(offset, this.yToExportRange(seg.endHandleY), true); offset += 2; view.setInt16(offset, this.yToExportRange(seg.endHandleY), true); offset += 2;
view.setInt16(offset, this.yToExportRange(seg.endPointY), true); offset += 2; view.setInt16(offset, this.yToExportRange(seg.endPointY), true); offset += 2;
console.log(seg.startPointY, seg.endPointY);
console.log(this.yToExportRange(seg.startPointY), this.yToExportRange(seg.endPointY)); console.log(this.yToExportRange(seg.startPointY), this.yToExportRange(seg.endPointY));
}); });
//console.log("🧵 Curve segments packed:", curveSegments.length); //console.log("🧵 Curve segments packed:", curveSegments.length);
//console.log(curveSegments);
return new Uint8Array(buffer.slice(0, offset)); return new Uint8Array(buffer.slice(0, offset));
@ -140,6 +141,7 @@ export class CurveEditor {
loadCurveSets(curveSets) { loadCurveSets(curveSets) {
this.curveSets = [] this.curveSets = []
this.curveSets = curveSets; this.curveSets = curveSets;
console.log(curveSets);
// If selectedMotorID is present in the new set, load its curves // If selectedMotorID is present in the new set, load its curves
if (curveSets[this.selectedMotorID]) { if (curveSets[this.selectedMotorID]) {
@ -148,8 +150,11 @@ export class CurveEditor {
this.setCurves([]); // fallback to empty this.setCurves([]); // fallback to empty
} }
console.log("LOADED"); console.log("LOADED");
console.log() console.log(this.curveSets);
setSelectedMotor(10); // Global defined in script.js console.log(Object.keys(curveSets)[0]);
setSelectedMotor(parseInt(Object.keys(curveSets)[0]));
// hello
//this.selectAdjacentMotor(1); //this.selectAdjacentMotor(1);
// Optional: update motor selector UI or redraw timeline // Optional: update motor selector UI or redraw timeline
//this.refreshMotorSelector?.(); // if you have a method for that //this.refreshMotorSelector?.(); // if you have a method for that
@ -168,35 +173,39 @@ export class CurveEditor {
valueToY(v) { // valueToY(v) {
return (this.canvas.height / 2 - v * (this.canvas.height / 2)) * this.scaleY + this.offset.y; // return (this.canvas.height / 2 - v * (this.canvas.height / 2)) * this.scaleY + this.offset.y;
// }
valueToYNoOffset(v) {
return (this.canvas.height / 2 - v * (this.canvas.height / 2));
} }
// Maps pixel value of y axis to -1 to 1 normalised value valueToY(v) {
yToValue(y) { return (this.logicalHeight / 2 - v * (this.logicalHeight / 2)) * this.scaleY + this.offset.y;
return ((this.canvas.height / 2 - (y - this.offset.y) / this.scaleY) / (this.canvas.height / 2));
} }
yToValue(y) {
return ((this.logicalHeight / 2 - (y - this.offset.y) / this.scaleY) / (this.logicalHeight / 2));
}
// Maps normalised -1 to 1 value to motor range (0, 4095) // Maps normalised -1 to 1 value to motor range (0, 4095)
yToExportRange(y) { yToExportRange(y) {
const [minOut, maxOut] = this.exportRange; const [minOut, maxOut] = this.exportRange;
// Normalize y from [-1, 1] to [0, 1] const clampedY = Math.max(0, Math.min(y, this.logicalHeight)); // optional safety
const normalized = (this.yToValue(y) + 1) / 2; const normalized = clampedY / this.logicalHeight; // maps to [0, 1]
// Scale to export range return Math.round(normalized * (maxOut - minOut) + minOut); // maps to [minOut, maxOut]
return Math.round(normalized * (maxOut - minOut) + minOut);
} }
exportRangeToY(value) { exportRangeToY(value) {
const [minOut, maxOut] = this.exportRange; const [minOut, maxOut] = this.exportRange;
// Reverse the scaling const normalized = 1 - (value - minOut) / (maxOut - minOut); // flipped
const normalized = (value - minOut) / (maxOut - minOut); return this.valueToYNoOffset(normalized * 2 - 1);
// Convert from [0, 1] back to [-1, 1]
const yValue = normalized * 2 - 1;
// Apply inverse mapping from value space to editor Y
return this.valueToY(yValue);
} }
valueToX(value) { valueToX(value) {
return value * this.pixelsPerSecond * this.scaleX + this.offset.x; return value * this.pixelsPerSecond * this.scaleX + this.offset.x;
} }

View File

@ -595,7 +595,8 @@ window.onload = () => {
//console.log(startTime, endTime, startPointY, endPointY); //console.log(startTime, endTime, startPointY, endPointY);
const toFloat = v => (v / 65535) * 2 - 1; const toFloat = v => (v / 65535) * 2 - 1;
console.log(startPointY);
console.log(curveEditor.exportRangeToY(startPointY));
const curve = { const curve = {
startPoint: { startPoint: {
x: startTime, x: startTime,