DigiDoodles ⚜️

PixelArt: Digital Doodles
Please adjust the settings to see what happens!

DigiDoodles ⚜️ (variation)
DigiDoodles ⚜️ (variation)
DigiDoodles ⚜️ (variation)

Log in to post a comment.

const gridSize = 8; //min=1 max=20 step=1
const spacing = 7; //min=0 max=20 step=.2
const margin = 28; //min=0 max=50 step=1
const doodleSize = 8; //min=2 max=30 step=1
const min_saturation = .55; //min=0 max=1 step=.01
const pixelStyle = 0; //min=0 max=2 step=1 (Hatching, Outline, Both)
const symm_vert = 1; //min=0 max=1 step=1 (No, Yes)
const symm_hori = 0; //min=0 max=1 step=1 (No, Yes)
const symm_diag_left = 0; //min=0 max=1 step=1 (No, Yes)
const symm_diag_right = 0; //min=0 max=1 step=1 (No, Yes)
const symm_rot_half = 0; //min=0 max=1 step=1 (No, Yes)
const symm_rot_quad = 0; //min=0 max=1 step=1 (No, Yes)
const plotPenSize = .15; //min=.01 max=1 step=.01

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();
const polygons = new Polygons();

const grid = (new Grid(gridSize, spacing, 200 - margin - margin)).iterator();

class PixelSticker {
    static SYMMETRY_HORIZONTAL = 1;
    static SYMMETRY_VERTICAL = 2;
    static SYMMETRY_ROTATION_QUAD = 4;
    static SYMMETRY_DIAGONAL_LEFT = 8;
    static SYMMETRY_DIAGONAL_RIGHT = 16;
    static SYMMETRY_ROTATION_HALF = 32;
    
    constructor(size, symmetry) {
        this.pattern = Array.from({length:size**2}).map(i => false);
        this.size = size;
        this.symmetry = symmetry;
        
        this.sym_h = (this.symmetry & this.constructor.SYMMETRY_HORIZONTAL) == this.constructor.SYMMETRY_HORIZONTAL;
        this.sym_v = (this.symmetry & this.constructor.SYMMETRY_VERTICAL) == this.constructor.SYMMETRY_VERTICAL;
        this.symm_rot_half = (this.symmetry & this.constructor.SYMMETRY_ROTATION_HALF) == this.constructor.SYMMETRY_ROTATION_HALF;
        this.symm_rot_quad = (this.symmetry & this.constructor.SYMMETRY_ROTATION_QUAD) == this.constructor.SYMMETRY_ROTATION_QUAD;
        this.sym_diag_left = (this.symmetry & this.constructor.SYMMETRY_DIAGONAL_LEFT) == this.constructor.SYMMETRY_DIAGONAL_LEFT;
        this.sym_diag_right = (this.symmetry & this.constructor.SYMMETRY_DIAGONAL_RIGHT) == this.constructor.SYMMETRY_DIAGONAL_RIGHT;
    }
    set(col, row, value) {
        this.pattern[col + row*this.size] = value;
    }
    diagSet(col, row, value) {
        const half = Math.floor(this.size/2);
        const oddSize = this.size%2==1;
        
        const mCol = -(col - half) + half - (oddSize?0:1);
        const mRow = -(row - half) + half - (oddSize?0:1);

        this.set(col, row, value);
        if(this.sym_diag_right) {
            this.set(mRow, mCol, value);
        }
        if(this.sym_diag_left) {
            this.set(row, col, value);
        }
        if(this.sym_diag_right && this.sym_diag_left) {
            this.set(mCol, mRow, value);
        }
    }
    rotSet(col, row, value) {
        this.diagSet(col, row, value);

        if(this.symm_rot_quad) {
            let rRow = row;
            let rCol = col;
            
            for(let i = 0; i < 3; i++) {
                const tmp = rCol;
                rCol = this.size - rRow - 1;
                rRow = tmp;

                this.diagSet(rCol, rRow, value);
           }
        }
        if(this.symm_rot_half) {
            const half = Math.floor(this.size/2);
            const oddSize = this.size%2==1;
            
            const mCol = -(col - half) + half - (oddSize?0:1);
            const mRow = -(row - half) + half - (oddSize?0:1);
            
            this.diagSet(mCol, mRow, value);
        }
    }
    enable(col, row) {
        this.rotSet(col, row, true);
        
        const half = Math.floor(this.size/2);
        const oddSize = this.size%2==1;
        
        const mCol = -(col - half) + half - (oddSize?0:1);
        const mRow = -(row - half) + half - (oddSize?0:1);
        
        if(this.sym_v) {
            this.rotSet(mCol, row, true);
        }
        if(this.sym_h) {
            this.rotSet(col, mRow, true);
        }
        if(this.sym_v && this.sym_h) {
            this.rotSet(mCol, mRow, true);
        }
    }
    value(col, row) {
        return this.pattern[col + row*this.size];
    }
    getSaturation() {
        return this.pattern.filter(i => i).length / this.size**2;
    }
}

const symmetry_setting = 
    (symm_vert == 1? PixelSticker.SYMMETRY_VERTICAL: 0) |
    (symm_hori == 1? PixelSticker.SYMMETRY_HORIZONTAL: 0) |
    (symm_diag_left == 1? PixelSticker.SYMMETRY_DIAGONAL_LEFT: 0) |
    (symm_diag_right == 1? PixelSticker.SYMMETRY_DIAGONAL_RIGHT: 0) |
    (symm_rot_quad == 1? PixelSticker.SYMMETRY_ROTATION_QUAD: 0) |
    (symm_rot_half == 1? PixelSticker.SYMMETRY_ROTATION_HALF: 0) |
0;

// The walk function will be called until it returns false.
function walk() {
    const cell = grid.next();
    if(cell.done) return false;

    const ps = new PixelSticker(doodleSize, symmetry_setting);
    while(ps.getSaturation() < min_saturation) {
        ps.enable((Math.random() * doodleSize)|0, (Math.random() * doodleSize)|0 );
    }

    let i = 0;
    for(const cellGrid of (new Grid(doodleSize, 0, cell.value.size, cell.value.center)).iterator()) {
        if(ps.pattern[i]) {
            const p = polygons.create();
            p.addPoints(...squarize(cellGrid.center, cellGrid.size));
            if(pixelStyle == 1 || pixelStyle == 2) p.addOutline();
            if(pixelStyle % 2 == 0) p.addHatching(1, plotPenSize);
            polygons.draw(turtle, p);
        }
        i++;
    }
    
    return true;
}

function squarize(center, size) {
    const add=(a,b)=>[a[0]+b[0],a[1]+b[1]];
    const scale=(a,s)=>[a[0]*s,a[1]*s];
    return [[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]].map(p => add(center,scale(p,size)));
}

////////////////////////////////////////////////////////////////
// Square Grid utility code - Created by Jurgen Westerhof 2023
// https://turtletoy.net/turtle/4633bef396
////////////////////////////////////////////////////////////////
function Grid(size, spacing, canvasSize = 200, location = [0, 0], cellProperties = {}) {
    function add2(a, b) { return [a[0]+b[0], a[1]+b[1]]; }
    class Grid {
        constructor(size, spacing, canvasSize, location, cellProperties) {
            this.size = size;this.spacing = spacing;this.cellProperties = cellProperties;this.iterateMax = this.size**2 - 1;
            this.canvasSize = canvasSize;this.location = location;this.cellSize = (Math.max(0.1, (canvasSize - (size-1)*spacing)) / size);
        }
        getCellByIndex(i) {const col = i % this.size;const row = i / this.size | 0;return this.getCellByCoord(col, row);}
        getCellByCoord(col, row) {const vertice = (i) => (i + .5) * this.cellSize + i * this.spacing - this.canvasSize / 2;return {center: add2(this.location, [vertice(col), vertice(row)]),column: col,row: row,index: row * this.size + col,size: this.cellSize,spacing: this.spacing,properties: this.cellProperties,};}
        *iterator() {
            for(let i = 0; i <= this.iterateMax; i++) {
                yield this.getCellByIndex(i);
            }
        }
    }
    return new Grid(size, spacing, canvasSize, location, cellProperties);
}

////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// (Polygon binning by Lionel Lemarie 2021)
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons(){const t=[],s=25,e=Array.from({length:s**2},t=>[]),n=class{constructor(){this.cp=[],this.dp=[],this.aabb=[]}addPoints(...t){let s=1e5,e=-1e5,n=1e5,h=-1e5;(this.cp=[...this.cp,...t]).forEach(t=>{s=Math.min(s,t[0]),e=Math.max(e,t[0]),n=Math.min(n,t[1]),h=Math.max(h,t[1])}),this.aabb=[s,n,e,h]}addSegments(...t){t.forEach(t=>this.dp.push(t))}addOutline(){for(let t=0,s=this.cp.length;t<s;t++)this.dp.push(this.cp[t],this.cp[(t+1)%s])}draw(t){for(let s=0,e=this.dp.length;s<e;s+=2)t.jump(this.dp[s]),t.goto(this.dp[s+1])}addHatching(t,s){const e=new n;e.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);const h=Math.sin(t)*s,o=Math.cos(t)*s,a=200*Math.sin(t),i=200*Math.cos(t);for(let t=.5;t<150/s;t++)e.dp.push([h*t+i,o*t-a],[h*t-i,o*t+a]),e.dp.push([-h*t+i,-o*t-a],[-h*t-i,-o*t+a]);e.boolean(this,!1),this.dp=[...this.dp,...e.dp]}inside(t){let s=0;for(let e=0,n=this.cp.length;e<n;e++)this.segment_intersect(t,[.1,-1e3],this.cp[e],this.cp[(e+1)%n])&&s++;return 1&s}boolean(t,s=!0){const e=[];for(let n=0,h=this.dp.length;n<h;n+=2){const h=this.dp[n],o=this.dp[n+1],a=[];for(let s=0,e=t.cp.length;s<e;s++){const n=this.segment_intersect(h,o,t.cp[s],t.cp[(s+1)%e]);!1!==n&&a.push(n)}if(0===a.length)s===!t.inside(h)&&e.push(h,o);else{a.push(h,o);const n=o[0]-h[0],i=o[1]-h[1];a.sort((t,s)=>(t[0]-h[0])*n+(t[1]-h[1])*i-(s[0]-h[0])*n-(s[1]-h[1])*i);for(let n=0;n<a.length-1;n++)(a[n][0]-a[n+1][0])**2+(a[n][1]-a[n+1][1])**2>=.001&&s===!t.inside([(a[n][0]+a[n+1][0])/2,(a[n][1]+a[n+1][1])/2])&&e.push(a[n],a[n+1])}}return(this.dp=e).length>0}segment_intersect(t,s,e,n){const h=(n[1]-e[1])*(s[0]-t[0])-(n[0]-e[0])*(s[1]-t[1]);if(0===h)return!1;const o=((n[0]-e[0])*(t[1]-e[1])-(n[1]-e[1])*(t[0]-e[0]))/h,a=((s[0]-t[0])*(t[1]-e[1])-(s[1]-t[1])*(t[0]-e[0]))/h;return o>=0&&o<=1&&a>=0&&a<=1&&[t[0]+o*(s[0]-t[0]),t[1]+o*(s[1]-t[1])]}};return{list:()=>t,create:()=>new n,draw:(n,h,o=!0)=>{reducedPolygonList=function(n){const h={},o=200/s;for(var a=0;a<s;a++){const c=a*o-100,r=[0,c,200,c+o];if(!(n[3]<r[1]||n[1]>r[3]))for(var i=0;i<s;i++){const c=i*o-100;r[0]=c,r[2]=c+o,n[0]>r[2]||n[2]<r[0]||e[i+a*s].forEach(s=>{const e=t[s];n[3]<e.aabb[1]||n[1]>e.aabb[3]||n[0]>e.aabb[2]||n[2]<e.aabb[0]||(h[s]=1)})}}return Array.from(Object.keys(h),s=>t[s])}(h.aabb);for(let t=0;t<reducedPolygonList.length&&h.boolean(reducedPolygonList[t]);t++);h.draw(n),o&&function(n){t.push(n);const h=t.length-1,o=200/s;e.forEach((t,e)=>{const a=e%s*o-100,i=(e/s|0)*o-100,c=[a,i,a+o,i+o];c[3]<n.aabb[1]||c[1]>n.aabb[3]||c[0]>n.aabb[2]||c[2]<n.aabb[0]||t.push(h)})}(h)}}}