From 503c3aa2f92d343275c9df87af06a3d47485eb63 Mon Sep 17 00:00:00 2001 From: Shoofle Date: Wed, 29 Jun 2016 14:56:42 -0400 Subject: [PATCH] clean up the comic layout generator --- articles/comic_layout_generator.article.html | 123 +++++++++++++------ 1 file changed, 86 insertions(+), 37 deletions(-) diff --git a/articles/comic_layout_generator.article.html b/articles/comic_layout_generator.article.html index d9ae543..5dcdfb6 100644 --- a/articles/comic_layout_generator.article.html +++ b/articles/comic_layout_generator.article.html @@ -6,13 +6,17 @@ var max_aspect_ratio = 3; $(document).ready(function() { clear(document.getElementById('svg')); }); +var ns = "http://www.w3.org/2000/svg"; +var margin = 5; +var max_aspect_ratio = 3; + +clear(document.getElementById('svg')); + function clear(panel) { while (panel.children.length > 0) { panel.children[0].remove(); } - var rect = new_rect(50, 50, 400, 600); - rect.setAttribute('id', 'panel'); - panel.appendChild(rect); + panel.appendChild(new_rect(50, 50, 400, 600)); } function generate() { @@ -24,29 +28,59 @@ function generate() { } function mutate(panel) { - var horizontal = is_split_horizontal(panel); - var portion = get_split_length(panel, horizontal); - var results = split(panel, horizontal, portion); - return results; + choose(split_types(panel))(panel); +} + +function split_types(panel) { + var splits = []; + + var aspect_ratio = get_aspect_ratio(panel); + if (aspect_ratio < 5) { + splits.push(split_vertical); + splits.push(multisplit_vertical); + } + + if (aspect_ratio > 0.2) { + splits.push(split_horizontal); + splits.push(multisplit_horizontal); + } + + return splits; } -function split(panel, horizontal, distance) { +function multisplit_vertical(panel) { var x = parseFloat(panel.getAttribute('x')); var y = parseFloat(panel.getAttribute('y')); var width = parseFloat(panel.getAttribute('width')); var height = parseFloat(panel.getAttribute('height')); - - var child_one, child_two; - if (horizontal) { - child_one = new_rect(x, y, distance - margin, height); - child_two = new_rect(x + distance + margin, y, - width - distance - margin, height); - } else { - child_one = new_rect(x, y, width, distance - margin); - child_two = new_rect(x, y + distance + margin, - width, height - distance - margin); + + var n = choose([2,2,3,3,3,4]); + + var results = []; + var step = (height + (1-n)*2*margin) / n; + + for (var i=0; i max_aspect_ratio) { - return true; - } else if ((1/aspect_ratio) > max_aspect_ratio) { - return false; + for (var i=0; i 0.5; + panel.remove(); + return results; } -function get_split_length(panel, horizontal) { - var dimension = panel.getAttribute(horizontal ? 'width' : 'height'); - var value = Math.random() * (dimension - 30*margin) + (15*margin); - return value; +function split_horizontal(panel) { + var x = parseFloat(panel.getAttribute('x')); + var y = parseFloat(panel.getAttribute('y')); + var width = parseFloat(panel.getAttribute('width')); + var height = parseFloat(panel.getAttribute('height')); + + var distance = Math.random()*(width - 2*margin) + margin; + + var child_one = new_rect(x, y, distance - margin, height); + var child_two = new_rect(x + distance + margin, y, + width - distance - margin, height); + + panel.parentElement.appendChild(child_one); + panel.parentElement.appendChild(child_two); + panel.remove(); + + return [child_one, child_two]; } function new_rect(x, y, width, height) { @@ -87,9 +137,8 @@ function new_rect(x, y, width, height) { return rect; } -function get_aspect_ratio(panel) { - return parseFloat(panel.getAttribute('width')) / parseFloat(panel.getAttribute('height')); -} +function get_aspect_ratio(p) {return parseFloat(p.getAttribute('width')) / parseFloat(p.getAttribute('height'));} +function choose(o) {return o[Math.floor(Math.random() * o.length)];}

Here, have a thing that generates layouts for comic pages! It's very rough, and silly.