Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** ---------------------------------------------- */
- let timer = null;
- let time = 0;
- function requestAnimationFrame(f) {
- timer = setImmediate(() => {
- time++;
- f(Date.now());
- });
- }
- const fs = require('fs');
- const jsnes = require('../../jsnes-web/node_modules/jsnes');
- const { createCanvas } = require('canvas');
- var SCREEN_WIDTH = 256;
- var SCREEN_HEIGHT = 240;
- var FRAMEBUFFER_SIZE = SCREEN_WIDTH * SCREEN_HEIGHT;
- var canvas_ctx, image;
- var framebuffer_u8, framebuffer_u32;
- var nes = new jsnes.NES({
- onFrame: function (framebuffer_24) {
- for (var i = 0; i < FRAMEBUFFER_SIZE; i++) framebuffer_u32[i] = 0xff000000 | framebuffer_24[i];
- },
- });
- function onAnimationFrame() {
- requestAnimationFrame(onAnimationFrame);
- image.data.set(framebuffer_u8);
- canvas_ctx.putImageData(image, 0, 0);
- let img = canvas_ctx.canvas.toDataURL();
- var data = img.replace(/^data:image\/\w+;base64,/, '');
- var buf = new Buffer(data, 'base64');
- fs.writeFileSync('image.png', buf);
- if (time >= 60 && timer !== null) {
- clearImmediate(timer);
- time = 0;
- process.exit();
- }
- }
- function nes_init() {
- const canvas = createCanvas(SCREEN_WIDTH, SCREEN_HEIGHT);
- canvas_ctx = canvas.getContext('2d');
- image = canvas_ctx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- canvas_ctx.fillStyle = 'black';
- canvas_ctx.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
- // Allocate framebuffer array.
- var buffer = new ArrayBuffer(image.data.length);
- framebuffer_u8 = new Uint8ClampedArray(buffer);
- framebuffer_u32 = new Uint32Array(buffer);
- }
- function nes_boot(rom_data) {
- nes.loadROM(rom_data);
- setInterval(() => {
- nes.frame();
- });
- requestAnimationFrame(onAnimationFrame);
- }
- function nes_load_url(romData) {
- nes_init();
- nes_boot(romData);
- }
- module.exports = nes_load_url;
- /** ---------------------------------------------- */
Add Comment
Please, Sign In to add comment