Update spline_editor.js

This commit is contained in:
kijai 2024-05-06 21:39:22 +03:00
parent 3b999778f0
commit 07ba67864a

View File

@ -207,97 +207,101 @@ app.registerExtension({
function createSplineEditor(context, reset=false) { function createSplineEditor(context, reset=false) {
console.log("creatingSplineEditor") console.log("creatingSplineEditor")
document.addEventListener('contextmenu', function(e) { // context menu
e.preventDefault(); function createContextMenu() {
}); document.addEventListener('contextmenu', function(e) {
document.addEventListener('click', function(e) {
if (!context.contextMenu.contains(e.target)) {
context.contextMenu.style.display = 'none';
}
});
context.menuItem1.addEventListener('click', function(e) {
e.preventDefault();
if (!drawHandles) {
drawHandles = true
vis.add(pv.Line)
.data(() => points.map((point, index) => ({
start: point,
end: [index]
})))
.left(d => d.start.x)
.top(d => d.start.y)
.interpolate("linear")
.tension(0) // Straight lines
.strokeStyle("#ff7f0e") // Same color as control points
.lineWidth(1)
.visible(() => drawHandles);
vis.render();
} else {
drawHandles = false
vis.render();
}
context.contextMenu.style.display = 'none';
});
context.menuItem2.addEventListener('click', function(e) {
e.preventDefault(); e.preventDefault();
drawSamplePoints = !drawSamplePoints; });
document.addEventListener('click', function(e) {
if (!context.contextMenu.contains(e.target)) {
context.contextMenu.style.display = 'none';
}
});
context.menuItem1.addEventListener('click', function(e) {
e.preventDefault();
if (!drawHandles) {
drawHandles = true
vis.add(pv.Line)
.data(() => points.map((point, index) => ({
start: point,
end: [index]
})))
.left(d => d.start.x)
.top(d => d.start.y)
.interpolate("linear")
.tension(0) // Straight lines
.strokeStyle("#ff7f0e") // Same color as control points
.lineWidth(1)
.visible(() => drawHandles);
vis.render();
} else {
drawHandles = false
vis.render();
}
context.contextMenu.style.display = 'none';
});
context.menuItem2.addEventListener('click', function(e) {
e.preventDefault();
drawSamplePoints = !drawSamplePoints;
updatePath();
});
context.menuItem3.addEventListener('click', function(e) {
e.preventDefault();
if (dotShape == "circle"){
dotShape = "triangle"
}
else {
dotShape = "circle"
}
console.log(dotShape)
updatePath(); updatePath();
}); });
context.menuItem3.addEventListener('click', function(e) { context.menuItem4.addEventListener('click', function(e) {
e.preventDefault(); // Create file input element
if (dotShape == "circle"){ const fileInput = document.createElement('input');
dotShape = "triangle" fileInput.type = 'file';
} fileInput.accept = 'image/*'; // Accept only image files
else {
dotShape = "circle"
}
console.log(dotShape)
updatePath();
});
context.menuItem4.addEventListener('click', function(e) { // Listen for file selection
// Create file input element fileInput.addEventListener('change', function(event) {
const fileInput = document.createElement('input'); const file = event.target.files[0]; // Get the selected file
fileInput.type = 'file';
fileInput.accept = 'image/*'; // Accept only image files
// Listen for file selection if (file) {
fileInput.addEventListener('change', function(event) { // Create a URL for the selected file
const file = event.target.files[0]; // Get the selected file const imageUrl = URL.createObjectURL(file);
if (file) { // Set the backgroundImage with the new URL and make it visible
// Create a URL for the selected file backgroundImage
const imageUrl = URL.createObjectURL(file); .url(imageUrl)
.visible(true)
.root.render();
}
});
// Set the backgroundImage with the new URL and make it visible // If the backgroundImage is already visible, hide it. Otherwise, show file input.
backgroundImage if (backgroundImage.visible()) {
.url(imageUrl) backgroundImage.visible(false)
.visible(true)
.root.render(); .root.render();
} } else {
}); // Trigger the file input dialog
fileInput.click();
// If the backgroundImage is already visible, hide it. Otherwise, show file input. }
if (backgroundImage.visible()) { context.contextMenu.style.display = 'none';
backgroundImage.visible(false) });
.root.render();
} else {
// Trigger the file input dialog
fileInput.click();
} }
context.contextMenu.style.display = 'none';
});
var dotShape = "circle"; var dotShape = "circle";
var drawSamplePoints = false; var drawSamplePoints = false;
createContextMenu();
function updatePath() { function updatePath() {
let coords = samplePoints(pathElements[0], points_to_sample, samplingMethod, w); let coords = samplePoints(pathElements[0], points_to_sample, samplingMethod, w);
@ -478,6 +482,7 @@ context.menuItem4.addEventListener('click', function(e) {
} }
}) })
var backgroundImage = vis.add(pv.Image) var backgroundImage = vis.add(pv.Image)
.visible(false)
vis.add(pv.Rule) vis.add(pv.Rule)
.data(pv.range(0, h, 64)) .data(pv.range(0, h, 64))
.bottom(d => d) .bottom(d => d)