mirror of
https://git.datalinker.icu/kijai/ComfyUI-KJNodes.git
synced 2025-12-10 21:34:43 +08:00
Update spline_editor.js
This commit is contained in:
parent
d24d1c64d2
commit
22f166008f
@ -135,44 +135,38 @@ app.registerExtension({
|
|||||||
menuItem.style.textDecoration = "none";
|
menuItem.style.textDecoration = "none";
|
||||||
menuItem.style.marginBottom = "5px";
|
menuItem.style.marginBottom = "5px";
|
||||||
}
|
}
|
||||||
this.menuItem1 = document.createElement("a");
|
function createMenuItem(id, textContent) {
|
||||||
this.menuItem1.href = "#";
|
let menuItem = document.createElement("a");
|
||||||
this.menuItem1.id = "menu-item-1";
|
menuItem.href = "#";
|
||||||
this.menuItem1.textContent = "Toggle handles";
|
menuItem.id = `menu-item-${id}`;
|
||||||
styleMenuItem(this.menuItem1);
|
menuItem.textContent = textContent;
|
||||||
|
styleMenuItem(menuItem);
|
||||||
|
return menuItem;
|
||||||
|
}
|
||||||
|
|
||||||
this.menuItem2 = document.createElement("a");
|
// Create an array of menu items using the createMenuItem function
|
||||||
this.menuItem2.href = "#";
|
this.menuItems = [
|
||||||
this.menuItem2.id = "menu-item-2";
|
createMenuItem(1, "Toggle handles"),
|
||||||
this.menuItem2.textContent = "Display sample points";
|
createMenuItem(2, "Display sample points"),
|
||||||
styleMenuItem(this.menuItem2);
|
createMenuItem(3, "Switch point shape"),
|
||||||
|
createMenuItem(4, "Background image"),
|
||||||
|
createMenuItem(5, "Invert point order")
|
||||||
|
];
|
||||||
|
|
||||||
this.menuItem3 = document.createElement("a");
|
// Add mouseover and mouseout event listeners to each menu item for styling
|
||||||
this.menuItem3.href = "#";
|
this.menuItems.forEach(menuItem => {
|
||||||
this.menuItem3.id = "menu-item-3";
|
menuItem.addEventListener('mouseover', function() {
|
||||||
this.menuItem3.textContent = "Switch point shape";
|
|
||||||
styleMenuItem(this.menuItem3);
|
|
||||||
|
|
||||||
this.menuItem4 = document.createElement("a");
|
|
||||||
this.menuItem4.href = "#";
|
|
||||||
this.menuItem4.id = "menu-item-4";
|
|
||||||
this.menuItem4.textContent = "Background image";
|
|
||||||
styleMenuItem(this.menuItem4);
|
|
||||||
|
|
||||||
const menuItems = [this.menuItem1, this.menuItem2, this.menuItem3, this.menuItem4];
|
|
||||||
|
|
||||||
menuItems.forEach(menuItem => {
|
|
||||||
menuItem.addEventListener('mouseover', function() {
|
|
||||||
this.style.backgroundColor = "gray";
|
this.style.backgroundColor = "gray";
|
||||||
});
|
});
|
||||||
menuItem.addEventListener('mouseout', function() {
|
|
||||||
|
menuItem.addEventListener('mouseout', function() {
|
||||||
this.style.backgroundColor = "#202020";
|
this.style.backgroundColor = "#202020";
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Append menu items to the context menu
|
// Append each menu item to the context menu
|
||||||
menuItems.forEach(menuItem => {
|
this.menuItems.forEach(menuItem => {
|
||||||
this.contextMenu.appendChild(menuItem);
|
this.contextMenu.appendChild(menuItem);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.body.appendChild( this.contextMenu);
|
document.body.appendChild( this.contextMenu);
|
||||||
@ -219,82 +213,88 @@ function createSplineEditor(context, reset=false) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
context.menuItem1.addEventListener('click', function(e) {
|
context.menuItems.forEach((menuItem, index) => {
|
||||||
e.preventDefault();
|
menuItem.addEventListener('click', function(e) {
|
||||||
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;
|
// Logic specific to each menu item based on its index or id
|
||||||
updatePath();
|
switch (index) {
|
||||||
});
|
case 0:
|
||||||
|
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';
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
e.preventDefault();
|
||||||
|
drawSamplePoints = !drawSamplePoints;
|
||||||
|
updatePath();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
e.preventDefault();
|
||||||
|
if (dotShape == "circle"){
|
||||||
|
dotShape = "triangle"
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
dotShape = "circle"
|
||||||
|
}
|
||||||
|
console.log(dotShape)
|
||||||
|
updatePath();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// Create file input element
|
||||||
|
const fileInput = document.createElement('input');
|
||||||
|
fileInput.type = 'file';
|
||||||
|
fileInput.accept = 'image/*'; // Accept only image files
|
||||||
|
|
||||||
context.menuItem3.addEventListener('click', function(e) {
|
// Listen for file selection
|
||||||
e.preventDefault();
|
fileInput.addEventListener('change', function(event) {
|
||||||
if (dotShape == "circle"){
|
const file = event.target.files[0]; // Get the selected file
|
||||||
dotShape = "triangle"
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
dotShape = "circle"
|
|
||||||
}
|
|
||||||
console.log(dotShape)
|
|
||||||
updatePath();
|
|
||||||
});
|
|
||||||
|
|
||||||
context.menuItem4.addEventListener('click', function(e) {
|
if (file) {
|
||||||
// Create file input element
|
// Create a URL for the selected file
|
||||||
const fileInput = document.createElement('input');
|
const imageUrl = URL.createObjectURL(file);
|
||||||
fileInput.type = 'file';
|
|
||||||
fileInput.accept = 'image/*'; // Accept only image files
|
|
||||||
|
|
||||||
// Listen for file selection
|
// Set the backgroundImage with the new URL and make it visible
|
||||||
fileInput.addEventListener('change', function(event) {
|
backgroundImage
|
||||||
const file = event.target.files[0]; // Get the selected file
|
.url(imageUrl)
|
||||||
|
.visible(true)
|
||||||
|
.root.render();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (file) {
|
// If the backgroundImage is already visible, hide it. Otherwise, show file input.
|
||||||
// Create a URL for the selected file
|
if (backgroundImage.visible()) {
|
||||||
const imageUrl = URL.createObjectURL(file);
|
backgroundImage.visible(false)
|
||||||
|
.root.render();
|
||||||
// Set the backgroundImage with the new URL and make it visible
|
} else {
|
||||||
backgroundImage
|
// Trigger the file input dialog
|
||||||
.url(imageUrl)
|
fileInput.click();
|
||||||
.visible(true)
|
}
|
||||||
.root.render();
|
context.contextMenu.style.display = 'none';
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
e.preventDefault();
|
||||||
|
points.reverse();
|
||||||
|
updatePath();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// If the backgroundImage is already visible, hide it. Otherwise, show file input.
|
|
||||||
if (backgroundImage.visible()) {
|
|
||||||
backgroundImage.visible(false)
|
|
||||||
.root.render();
|
|
||||||
} else {
|
|
||||||
// Trigger the file input dialog
|
|
||||||
fileInput.click();
|
|
||||||
}
|
|
||||||
context.contextMenu.style.display = 'none';
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user