131 lines
4.8 KiB
HTML
131 lines
4.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Compositor4 Example</title>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.5.0/fabric.min.js"></script>
|
|
<script type="module" src="toolbar4.js"></script>
|
|
<script type="module">
|
|
import Compositor4 from './compositor4.js';
|
|
import { Toolbar, ICON_URLS, BUTTON_INDICES, fetchSVGIcon } from './toolbar4.js';
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
const query = "matisse"; // Change this to your desired search query
|
|
try {
|
|
const images = await fetchArtInstituteImages(query, 6);
|
|
|
|
const PREFERENCES = {
|
|
composition: {
|
|
fill: "#444444", // Dark gray color for the rectangle
|
|
stroke: "green", // Green border
|
|
strokeWidth: 1,
|
|
},
|
|
toolbar: {
|
|
fill: "#666666",
|
|
height: 40,
|
|
position: { left: 0, top: 0 },
|
|
buttonSpacing: 4,
|
|
iconSize: 40, // Set the icon size to 30
|
|
},
|
|
fabricCanvas: {
|
|
backgroundColor: "#555555", // Darker gray color for the canvas background
|
|
},
|
|
equalizeHeight: {
|
|
enabled: true,
|
|
},
|
|
snapToGrid: {
|
|
gridSize: 20,
|
|
enabled: false,
|
|
isGridVisible: false,
|
|
},
|
|
ignoreTransparentPixels: {
|
|
enabled: false,
|
|
},
|
|
erosColor: "magenta", // Add eros color preference
|
|
activeBorderColor: "magenta", // Border color when button is active
|
|
inactiveBorderColor: "black", // Border color when button is inactive
|
|
toggledBorderColor: "orange", // Border color when button is toggled
|
|
standalone:true,
|
|
};
|
|
|
|
const config = JSON.stringify({
|
|
width: 800,
|
|
height: 600,
|
|
padding: 100,
|
|
seed: 12345,
|
|
onConfigChanged: "configChanged",
|
|
isConfigChanged: false,
|
|
preset: "{}",
|
|
images: images,
|
|
});
|
|
|
|
const compositor = new Compositor4(document.getElementById("compositor"), config, PREFERENCES);
|
|
// trigger the executed event
|
|
const event = new CustomEvent("executed", { detail: config });
|
|
document.dispatchEvent(event);
|
|
} catch (error) {
|
|
console.error("Error fetching images:", error);
|
|
}
|
|
});
|
|
|
|
async function fetchArtInstituteImages(query, count) {
|
|
const searchUrl = `https://api.artic.edu/api/v1/artworks/search?q=${encodeURIComponent(query)}`;
|
|
const response = await fetch(searchUrl);
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch data: ${response.statusText}`);
|
|
}
|
|
const data = await response.json();
|
|
const artworks = data.data.slice(0, count);
|
|
|
|
const imageUrls = await Promise.all(
|
|
artworks.map(async (artwork) => {
|
|
const artworkResponse = await fetch(artwork.api_link);
|
|
if (!artworkResponse.ok) {
|
|
console.warn(`Failed to fetch artwork details for ${artwork.id}: ${artworkResponse.statusText}`);
|
|
return null;
|
|
}
|
|
const artworkData = await artworkResponse.json();
|
|
const imageId = artworkData.data.image_id;
|
|
if (!imageId) {
|
|
console.warn(`No image_id found for artwork: ${artwork.id}`);
|
|
return null;
|
|
}
|
|
return `https://www.artic.edu/iiif/2/${imageId}/full/843,/0/default.jpg`;
|
|
})
|
|
);
|
|
|
|
return imageUrls.filter((url) => url !== null);
|
|
}
|
|
|
|
// Add a function here that creates a grid image with dots equally spaced every DISTANCE pixels where DISTANCE is a parameter. Do not make it a compositor method.
|
|
function createGridImageWithDots(distance) {
|
|
const canvas = document.createElement("canvas");
|
|
canvas.width = 800; // Example width
|
|
canvas.height = 600; // Example height
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
// Set the background to white
|
|
ctx.fillStyle = "white";
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
// Set the fill color for the dots to black
|
|
ctx.fillStyle = "black";
|
|
|
|
// Draw 1x1 pixel dots
|
|
for (let x = distance; x < canvas.width; x += distance) {
|
|
for (let y = distance; y < canvas.height; y += distance) {
|
|
ctx.fillRect(x, y, 1, 1);
|
|
}
|
|
}
|
|
|
|
document.body.appendChild(canvas);
|
|
}
|
|
|
|
// Example usage of the function
|
|
createGridImageWithDots(20);
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div id="compositor"></div>
|
|
</body>
|
|
</html> |