Open cdsmith opened 10 years ago
I really want to do this!
The big blocker isn't so much that the implementation is tricky, as that the semantics are unclear when computing the intersection, difference, or union of pictures with color and transparency. In an ideal world, we'd have separated "Shape" from "Picture", and the former would have no concept of color or transparency. But CodeWorld doesn't make that distinction, so we need a reasonable answer for union, intersection, and difference that incorporates color and transparency. I also want to preserve the property that coloring a shape with full transparency makes it equivalent to blank, and I want to use pictures
and &
instead of a distinct union, to avoid adding too many similar functions.
I think there may be a synergy between this and #1397. My theory on #1397 is that &
, which is currently used to overlay images, is just too much like the English word. One could imagine replacing it with a different operator. I might choose |+|
because it sort of looks like overlaying two images (from a side view). That opens the door to |*|
for intersection, and |-|
for subtraction.
I think this definitely needs field testing before making a decision. I'll have to see whether I can find a good setting for that field testing.
Additional operations should be added to combine shapes:
(Names are debatable.)
intersection
should produce a picture bounded by the intersection of the list. The second should produce a picture containing all areas of the first image that are not also in the second. Note that together with the existingpictures
, this gives the three basic operations for constructive geometry.The implementation is a little tricky. Combining two images in this way can be done with globalCompositeOperation (https://developer.mozilla.org/samples/canvas-tutorial/6_1_canvas_composite.html) But getting the resulting shapes to nest correctly will involve using off-screen canvases as temporaries. Since creating a new off-screen canvas is expensive, this should involve determining the minimum number of offscreen canvases needed, and reusing them between frames.