-
Notifications
You must be signed in to change notification settings - Fork 112
Offline image #1774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Offline image #1774
Conversation
This is surprisingly close; full output of test-images.arr at the end. I'll do another commit to show the diff between CPO images and the changes made here to make it work, but it's gratifyingly small as a diff. file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:16:0-22:3: Overlay equality (2/2) file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:24:0-51:3: Composing lists of images (9/10) line 29, column 2: ok line 31, column 2: ok line 33, column 2: ok line 35, column 2: ok line 37, column 2: ok line 39, column 2: ok line 43, column 2: failed because: Got unexpected exception TypeError: Cannot set properties of undefined (setting 'visibility') TypeError: Cannot set properties of undefined (setting 'visibility') at BaseImage.difference (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170289:49) at Object.imageDifference (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:169942:19) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:183240:35) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__1 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:611:31) at PFunction._87a6d29cadb57d99ecea353caea55134445d06163c8bbe11473760ca1805bb72__1442 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:15500:27) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 45, column 2: ok line 47, column 2: ok line 49, column 2: ok file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:53:0-136:3: Polygons (76/76) file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:138:0-146:3: color-lists (5/5) file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:148:0-165:3: trimming (4/11) line 150, column 2: ok line 151, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170672:39) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__274 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:7587:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 152, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170672:39) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__277 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:7677:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 153, column 2: ok line 154, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170672:39) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__284 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:7865:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 157, column 2: ok line 158, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170663:42) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__291 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:8061:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 159, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170663:42) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__294 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:8151:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 160, column 2: ok line 161, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170663:42) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__301 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:8339:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) line 163, column 2: failed because: Got unexpected exception TypeError: Cannot read properties of undefined (reading 'createElement') TypeError: Cannot read properties of undefined (reading 'createElement') at trimCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170663:42) at Object.trimImageToCanvas (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:170624:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:184184:28) at PFunction._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__305 [as app] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:8437:25) at thisRuntime.run.sync (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162257:24) at ActivationRecord.fun (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161847:18) at iter (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at Object.run (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162064:7) at Pause.resumer (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:162256:21) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161991:41) at process.processImmediate (node:internal/timers:476:21) file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:167:0-189:3: properties (4/4) Check block ended in the following error (not all tests may have run): ReferenceError: document is not defined ReferenceError: document is not defined at getTextDimensions (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:171231:18) at new TextImage (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:171282:21) at Object.makeTextImage (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:171690:14) at PFunction.app (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:183279:32) at ActivationRecord._c408306ace046f838bbd554fa60d21cbef4ee2a8e6cc32b08191354d5e134c88__310 [as fun] (eval at <anonymous> (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:163823:32), <anonymous>:8941:24) at Immediate.iter [as _onImmediate] (/Users/joe/src/pyret-lang/tests/pyret/tests/test-images.jarr:161965:28) at process.processImmediate (node:internal/timers:476:21) file:///Users/joe/src/pyret-lang/tests/pyret/tests/test-images.arr:191:0-222:3: predicates (23/23) Passed: 123; Failed: 8; Ended in Error: 1; Total: 131
This commit will be immediately reverted; it has exactly the changes made to the files we started from in code.pyret.org
This reverts commit cfe58ec. That commit was made solely to show some meaningful diff information (but backwards, because the edits had already been made)
(This was meant to be a draft, tests still fail) |
The remaining issue is in It has a comment next to it that there should be That's next to look into. |
Even if the browsers are still unreliable (though it seems like maybe not now, as you said), it seems like node-canvas supports this, so could use it offline? https://github.com/Automattic/node-canvas/wiki/Compatibility-Status#drawing-text Either way, this is awesome! :) |
This issue has a nice picture, duplicated here: |
As a point of comparison,
|
Here we're using the `actualBoundingBox` to calculate text sizes. We update the tests a little to match this new behavior (since before they were more based on font sizes). More discussion here: #1774
This all of a sudden mattered b/c the node-canvas library relies on a newer GLIBC
- Buffer in Node has a nice toString('base64') method (unlike in browser) - FileReader doesn't exist natively in node - So use .toString('base64') offline and FileReader in browser Also, this polyfills Image (which is nicely provided by node-canvas)
- Always saves to .png format - Uses toBuffer on canvases and relies on fs.writeFile to handle the buffer
This is a little annoying and polyglot: toBuffer is super convenient and provided by node-canvas, but we have to go through Blobification in the browser.
I think this is close to ready to merge. It has the corresponding CPO changes in brownplt/code.pyret.org#585, which ends up having some related bits, because (via the VScode extension) the CPO code has to provide |
Progress on using
node-canvas
(https://www.npmjs.com/package/canvas) to make images work offline and in CLI mode (for, e.g., an autograder).This boils down to:
document.createElement("canvas")
and polyfilling withnodeCanvas.createCanvas
canvas.style
is used and working around it conditionally (offline canvases do not have styles; they aren't full DOM nodes)This is surprisingly straightforward and many tests pass already, including ones that render to canvases via BaseImage.prototype.equals (e.g. cropping a rectangle to a square and checking equality).
Other TODOs include:
image-url
image-file
is properly hooked up tofs
and the right combo of browser/CLI librariessave-image
function to actually view the images