Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div>
  <h3>Mori Painter</h3>
</div>
<div id='container'>
  <canvas id='canvas'></canvas>
</div>
<div>
  <button id='undo'>↶</button>
</div>
              
            
!

CSS

              
                body {
  font-family: monospace;  
}

canvas {
  position: relative;
}

#container{
  width: 200px;
  height: 200px;
  border: solid 5px #000;  
}
              
            
!

JS

              
                const {
  list, vector, peek, pop, conj, map, assoc, zipmap,
  range, repeat, each, intoArray, toJs, isEmpty,
  count
} = mori;

const log = (...args) => {
  console.log(...args.map(toJs));
};

// the dimensions of the canvas
const [height, width] = [20, 20];
// the size of each canvas pixel
const pixelSize = 10;

// converts an integer to a 2d coordinate
const to2D = (i) => vector(
  i % width,
  Math.floor(i / width)
);

// sequence of coordinates in the canvas
const coords = map(to2D, range(height * width));

// lazy sequence of colors
const colors = repeat('#fff');

// colors stored against coordinates
const pixels = zipmap(coords, colors);

// update pixels to reflect drawing at x, y
const draw = (x, y, pixels, color='#000') => {
  const coord = vector(x, y); 
  return assoc(pixels, coord, color);
};

// paint the values in pixels onto the ctx
const paint = (ctx, pixels) => {
  const px = pixelSize;
  each(pixels, p => {
    const [coord, color] = intoArray(p);
    const [x, y] = intoArray(coord);
    ctx.fillStyle = color;
    ctx.fillRect(x * px, y * px, px, px);
  });
};

const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const undo = document.getElementById('undo');

canvas.width = width * pixelSize;
canvas.height = height * pixelSize;
paint(context, pixels);

// keep track of current frames with rebindable variable
let frames = list(pixels);

canvas.addEventListener('click', e => {
  const x = Math.floor(e.layerX / pixelSize);
  const y = Math.floor(e.layerY / pixelSize);
  const pixels = draw(x, y, peek(frames));
  frames = conj(frames, pixels);
  paint(context, pixels)
});

undo.addEventListener('click', e => {
  if(count(frames) > 1) {
    frames = pop(frames); 
    paint(context, peek(frames));
  }
});

              
            
!
999px

Console