I have a javascript/typescript module for packing arbitrary data into a cartridge that I use for a handful of my projects. I recently published it as an npm package as I thought it may be helpful to more people.
Below is an example cart where I used this package to quickly prepare a cart with a payload of polygonal shape data, followed by the script that was used to build it.
(The resulting p8 cart was exported as a png cart separately)
Use ◀️/▶️/🅾️/❎ to change shape.
import { readFileSync, writeFileSync } from 'fs'; import { CartData, writeCart } from 'p8-data-cart'; const shapes = [ // Square shape [ -0.3, -0.3, 0.3, -0.3, 0.3, 0.3, -0.3, 0.3 ], // Star shape [ -0.33867, -0.11005, -0.07969, -0.11005, 0.00000, -0.35553, 0.07969, -0.11005, 0.33867, -0.11005, 0.12912, 0.04222, 0.20885, 0.28781, 0.00000, 0.13605, -0.20885, 0.28781, -0.12912, 0.04222 ], // Heart shape [ 0.27532, -0.04442, 0.25741, -0.01513, 0.23889, 0.01376, 0.21978, 0.04224, 0.20008, 0.07030, 0.17980, 0.09794, 0.15894, 0.12513, 0.13751, 0.15189, 0.11553, 0.17819, 0.09298, 0.20402, 0.06990, 0.22938, 0.04627, 0.25427, 0.02210, 0.27866, 0.01915, 0.28138, 0.01593, 0.28362, 0.01250, 0.28539, 0.00891, 0.28668, 0.00520, 0.28750, 0.00144, 0.28783, -0.00234, 0.28768, -0.00608, 0.28706, -0.00973, 0.28595, -0.01324, 0.28435, -0.01657, 0.28227, -0.01966, 0.27970, -0.01975, 0.27962, -0.01983, 0.27953, -0.01992, 0.27945, -0.02001, 0.27936, -0.02010, 0.27927, -0.02019, 0.27919, -0.02027, 0.27910, -0.02036, 0.27901, -0.02044, 0.27893, -0.02053, 0.27884, -0.02062, 0.27875, -0.02070, 0.27866, -0.04491, 0.25428, -0.06860, 0.22940, -0.09174, 0.20404, -0.11433, 0.17821, -0.13637, 0.15191, -0.15785, 0.12516, -0.17875, 0.09796, -0.19909, 0.07032, -0.21884, 0.04226, -0.23800, 0.01377, -0.25656, -0.01512, -0.27452, -0.04442, -0.29418, -0.09758, -0.29962, -0.14669, -0.29277, -0.19072, -0.27559, -0.22866, -0.25001, -0.25950, -0.21800, -0.28221, -0.18148, -0.29578, -0.14242, -0.29919, -0.10275, -0.29142, -0.06443, -0.27146, -0.02939, -0.23829, 0.00040, -0.19089, 0.03020, -0.23829, 0.06523, -0.27146, 0.10355, -0.29142, 0.14322, -0.29919, 0.18228, -0.29578, 0.21880, -0.28221, 0.25082, -0.25950, 0.27639, -0.22866, 0.29357, -0.19072, 0.30042, -0.14669, 0.29499, -0.09758 ] ]; function buildPayload(shapes: number[][]) { const payload: number[] = []; payload.push(shapes.length); // Shape count // Shape offset table let offset = 1 + shapes.length; // Shape count byte + offset table size for (const shapeData of shapes) { payload.push(offset); offset += 1 + shapeData.length; // 1 byte for point count + length of point data } // Shape point data for (const shapeData of shapes) { const pointCount = shapeData.length / 2; payload.push(pointCount); for (const shapePointComponent of shapeData) { // Center points around 0.5, 0.5 and scale range -1 - 1 to 0 - 255 const ratio = (255 - 0) / (1 - -1) const centeredComponent = (shapePointComponent + 1); const scaledComponent = Math.round(centeredComponent * ratio); payload.push(scaledComponent); } } return new Uint8Array(payload); } const luaScript = readFileSync('shapes.lua', 'utf-8'); const payload = buildPayload(shapes); const cartData = new CartData(); cartData.data.set(payload); cartData.lua = luaScript; const cartString = writeCart(cartData); writeFileSync('shapes.p8', cartString); |
[Please log in to post a comment]