var CANVAS, CTX, SHEET; var PALETTE; function resizeCanvas () { CANVAS.width = document.innerWidth / 2; CANVAS.height = document.innerHeight / 2; } function save () { var zde = CANVAS.toDataURL ("image/jpeg"); var img = new Image (); img.onload = function () { document.forms["save"].imgSrc.value=zde; document.forms["save"].submit (); } img.src = zde; } function main () { document.getElementById("zde").onclick=save; CANVAS = document.getElementById("canvas"); NAVIGATION = Navigation (); NAVIGATION.setMouseEvents(); CTX = CANVAS.getContext("2d"); PALETTE = Palette (); SHEET.penrose(12); } /* style = true : aigu style = false : obtus */ function get_color(style) { return style ? "#79B2E3" : "yellow"; } var PHI = (1.+Math.sqrt(5.))*0.5; function barycentre (a, P, b, Q) { return [a*P[0] + b*Q[0], a*P[1] + b*Q[1]]; } var Triangle = function (P, Q, R, style, prof) { var that = {}; that.P = P; that.Q = Q; that.R = R; that.style = style; that.color = get_color(that.style); that.prof = prof; // returns S that.split=function () { return that.style ? barycentre (2-PHI, that.P, PHI-1, that.Q) : barycentre(PHI-1, that.Q, 2-PHI, that.R); } that.draw = function () { SHEET.moveTo(that.P[0], that.P[1]); SHEET.lineTo(that.Q[0], that.Q[1]); SHEET.lineTo(that.R[0], that.R[1]); SHEET.lineTo(that.P[0], that.P[1]); } return that; }; var SHEET; var Sheet = function () { var that = {}; that.typeSelected = true; that.colorAigu = get_color(true); that.colorObtus = get_color(false); // ---------------------------------------------- DRAW METHODS ------------------------------------------- that.zoom = 1. ; that.offset = [0., 0.]; that.lineTo = function (a, b) { CTX.lineTo(Math.round(a*that.zoom + that.offset[0]), Math.round(b*that.zoom + that.offset[1])); } that.moveTo = function (a, b) { CTX.moveTo(Math.round(a*that.zoom + that.offset[0]), Math.round(b*that.zoom + that.offset[1])); } that.clearRect = function (a, b, c, d) { CTX.clearRect(a, b, c, d); } that.beginPath = function () { CTX.beginPath(); } that.closePath = function () { CTX.closePath(); } that.stroke = function () { CTX.stroke () ; } that.fill = function () { CTX.fill () ; } that.fillText = function (text, posx, posy) { CTX.fillText(text, posx, posy); } // ---------------------------------------------- PENROSE ------------------------------------------- that.drawstackAigu = []; that.drawstackObtus = []; that.loaded = false; that.lvl = 10; that.changeColor = function (style, color) { if (style) that.colorAigu = color; else that.colorObtus = color; } that.penrose = function(lvl) { that.lvl = lvl; document.getElementById("iter").innerHTML = "Number of iterations " + that.lvl ; that.loaded = false; that.drawstackAigu = []; that.drawstackObtus = []; var h = CANVAS.height; var w = CANVAS.width; var margin = 0.15; var Q = [margin*w, margin*h]; var R = [margin*w, (1-margin)*h]; var x = (1-2*margin)*h/2; var hh = Math.sqrt(4*(PHI+1)*x*x - x*x); var P = [hh+margin*w, 0.5*h]; var root = Triangle(P, Q, R, true, lvl); var stack = []; stack.push(root); while (stack.length > 0) { var zde = stack.pop (); if (zde.prof <= 1) { if (zde.style) that.drawstackAigu.push(zde); else that.drawstackObtus.push(zde); } else { var S = zde.split (); if (zde.style) { stack.push(Triangle(zde.R, S, zde.Q, true, zde.prof-2)); stack.push(Triangle(S, zde.P, zde.R, false, zde.prof-1)); } else { stack.push(Triangle(zde.R, zde.P, S, true, zde.prof-1)); stack.push(Triangle(S, zde.P, zde.Q, false, zde.prof-2)); } } } that.loaded = true; that.draw (); } that.draw = function () { if (that.loaded) { that.clearRect(0, 0, CANVAS.width, CANVAS.height); CTX.fillStyle = that.colorAigu; CTX.strokeStyle = "lightgrey"; that.beginPath(); for (var i = 0 ; i < that.drawstackAigu.length; ++i) { that.drawstackAigu[i].draw (); } that.closePath(); that.fill(); that.stroke(); CTX.fillStyle = that.colorObtus; that.beginPath(); for (var i = 0 ; i < that.drawstackObtus.length; ++i) { that.drawstackObtus[i].draw (); } that.closePath(); that.fill(); that.stroke(); } } return that; }; SHEET = Sheet ();var DRAG = false; function getY( oElement ){ var iReturnValue = 0; while( oElement != null ) { iReturnValue += oElement.offsetTop; oElement = oElement.offsetParent; } return iReturnValue; } //retourne la coordonnée X d'un élément function getX( oElement ){ var iReturnValue = 0; while( oElement != null ) { iReturnValue += oElement.offsetLeft; oElement = oElement.offsetParent; } return iReturnValue; } var Navigation = function () { var that = {}; that.oldX = 0 ; that.oldY = 0 ; that.XX = getX(document.getElementById("palette")); that.YY = getY(document.getElementById("palette")); that.handleMouseUp = function (e) { DRAG = false; } that.handleMouseDown = function (e) { DRAG = true; } that.handleMouseMove = function (e) { var x = e.clientX; var y = e.clientY; if (DRAG) { SHEET.offset[0] += x-that.oldX SHEET.offset[1] += y-that.oldY; } that.oldX = x; that.oldY = y; SHEET.draw (); } that.handleMouseRoll=function(e) { var roll; if (e.detail) { roll=e.detail; //Firefox } else { roll=-0.025*e.wheelDelta; //chrome } var oldZoom = SHEET.zoom; SHEET.zoom -= 0.01*roll; SHEET.offset[0] += 0.5*CANVAS.width*(oldZoom - SHEET.zoom); SHEET.offset[1] += 0.5*CANVAS.height*(oldZoom - SHEET.zoom); SHEET.draw (); } that.handleMouseOut = function (e) { DRAG = false; } that.setMouseEvents = function () { CANVAS.onmousewheel = that.handleMouseRoll; //pour chrome CANVAS.onmousemove = that.handleMouseMove; //pour chrome CANVAS.onmouseup = that.handleMouseUp; //pour chrome CANVAS.onmouseout = that.handleMouseOut; CANVAS.onmousedown = that.handleMouseDown; //pour chrome window.addEventListener('DOMMouseScroll', that.handleMouseRoll, false); //pour FF } return that; }; function hslToRGB(h, s, l){ h = h/360; s = s/100; l = l/100; if(s == 0){ r = g = b = l; // achromatic } else{ function hue2rgb(p, q, t){ if(t < 0) t += 1; if(t > 1) t -= 1; if(t < 1/6) return p + (q - p) * 6 * t; if(t < 1/2) return q; if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; return p; } var q = l < 0.5 ? l * (1 + s) : l + s - l * s; var p = 2 * l - q; r = hue2rgb(p, q, h + 1/3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1/3); } return "rgb(" + parseInt(r * 255) + ", " + parseInt(g * 255) + ", " + parseInt(b * 255) + ")"; } function activeColor(x, y){ var h = parseInt(((parseInt(x) + 7) * 360)/206); var l = parseInt(((parseInt(y) + 20) * 80)/60); var s = 100; color = hslToRGB(h,s,l); return color; } var Palette = function () { var that = {}; that.clicked = false; that.color = "rgb(0, 0, 0)"; that.element = document.getElementById("palette"); that.element.onmousemove = function (e) { if (!that.clicked) { var x = e.clientX-NAVIGATION.XX; var y = e.clientY - NAVIGATION.YY; var zde = activeColor(x, y); that.color = zde; document.getElementById("selected").style.backgroundColor = zde; } } that.element.onmousedown = function (e) { that.clicked = true; var x = e.clientX-NAVIGATION.XX; var y = e.clientY - NAVIGATION.YY; var zde = activeColor(x, y); that.color = zde; document.getElementById("selected").style.backgroundColor = zde; SHEET.changeColor(SHEET.typeSelected, that.color); SHEET.draw (); } that.element.onmouseout = function (e) { that.clicked = false; } return that; };/* html5slider - a JS implementation of for Firefox 4 and up https://github.com/fryn/html5slider Copyright (c) 2010-2011 Frank Yan, Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ (function() { // test for native support var test = document.createElement('input'); try { test.type = 'range'; if (test.type == 'range') return; } catch (e) { return; } // test for required property support if (!document.mozSetImageElement || !('MozAppearance' in test.style)) return; var scale; var isMac = navigator.platform == 'MacIntel'; var thumb = { radius: isMac ? 9 : 6, width: isMac ? 22 : 12, height: isMac ? 16 : 20 }; var track = '-moz-linear-gradient(top, transparent ' + (isMac ? '6px, #999 6px, #999 7px, #ccc 9px, #bbb 11px, #bbb 12px, transparent 12px' : '9px, #999 9px, #bbb 10px, #fff 11px, transparent 11px') + ', transparent)'; var styles = { 'min-width': thumb.width + 'px', 'min-height': thumb.height + 'px', 'max-height': thumb.height + 'px', padding: 0, border: 0, 'border-radius': 0, cursor: 'default', 'text-indent': '-999999px' // -moz-user-select: none; breaks mouse capture }; var onChange = document.createEvent('HTMLEvents'); onChange.initEvent('change', true, false); if (document.readyState == 'loading') document.addEventListener('DOMContentLoaded', initialize, true); else initialize(); function initialize() { // create initial sliders Array.forEach(document.querySelectorAll('input[type=range]'), transform); // create sliders on-the-fly document.addEventListener('DOMNodeInserted', onNodeInserted, true); } function onNodeInserted(e) { check(e.target); if (e.target.querySelectorAll) Array.forEach(e.target.querySelectorAll('input'), check); } function check(input, async) { if (input.localName != 'input' || input.type == 'range'); else if (input.getAttribute('type') == 'range') transform(input); else if (!async) setTimeout(check, 0, input, true); } function transform(slider) { var isValueSet, areAttrsSet, isChanged, isClick, prevValue, rawValue, prevX; var min, max, step, range, value = slider.value; // lazily create shared slider affordance if (!scale) { scale = document.body.appendChild(document.createElement('hr')); style(scale, { '-moz-appearance': isMac ? 'scale-horizontal' : 'scalethumb-horizontal', display: 'block', visibility: 'visible', opacity: 1, position: 'fixed', top: '-999999px' }); document.mozSetImageElement('__sliderthumb__', scale); } // reimplement value and type properties var getValue = function() { return '' + value; }; var setValue = function setValue(val) { value = '' + val; isValueSet = true; draw(); delete slider.value; slider.value = value; slider.__defineGetter__('value', getValue); slider.__defineSetter__('value', setValue); }; slider.__defineGetter__('value', getValue); slider.__defineSetter__('value', setValue); slider.__defineGetter__('type', function() { return 'range'; }); // sync properties with attributes ['min', 'max', 'step'].forEach(function(prop) { if (slider.hasAttribute(prop)) areAttrsSet = true; slider.__defineGetter__(prop, function() { return this.hasAttribute(prop) ? this.getAttribute(prop) : ''; }); slider.__defineSetter__(prop, function(val) { val === null ? this.removeAttribute(prop) : this.setAttribute(prop, val); }); }); // initialize slider slider.readOnly = true; style(slider, styles); update(); slider.addEventListener('DOMAttrModified', function(e) { // note that value attribute only sets initial value if (e.attrName == 'value' && !isValueSet) { value = e.newValue; draw(); } else if (~['min', 'max', 'step'].indexOf(e.attrName)) { update(); areAttrsSet = true; } }, true); slider.addEventListener('mousedown', onDragStart, true); slider.addEventListener('keydown', onKeyDown, true); slider.addEventListener('focus', onFocus, true); slider.addEventListener('blur', onBlur, true); function onDragStart(e) { isClick = true; setTimeout(function() { isClick = false; }, 0); if (e.button || !range) return; var width = parseFloat(getComputedStyle(this, 0).width); var multiplier = (width - thumb.width) / range; if (!multiplier) return; // distance between click and center of thumb var dev = e.clientX - this.getBoundingClientRect().left - thumb.width / 2 - (value - min) * multiplier; // if click was not on thumb, move thumb to click location if (Math.abs(dev) > thumb.radius) { isChanged = true; this.value -= -dev / multiplier; } rawValue = value; prevX = e.clientX; this.addEventListener('mousemove', onDrag, true); this.addEventListener('mouseup', onDragEnd, true); } function onDrag(e) { var width = parseFloat(getComputedStyle(this, 0).width); var multiplier = (width - thumb.width) / range; if (!multiplier) return; rawValue += (e.clientX - prevX) / multiplier; prevX = e.clientX; isChanged = true; this.value = rawValue; } function onDragEnd() { this.removeEventListener('mousemove', onDrag, true); this.removeEventListener('mouseup', onDragEnd, true); } function onKeyDown(e) { if (e.keyCode > 36 && e.keyCode < 41) { // 37-40: left, up, right, down onFocus.call(this); isChanged = true; this.value = value + (e.keyCode == 38 || e.keyCode == 39 ? step : -step); } } function onFocus() { if (!isClick) this.style.boxShadow = !isMac ? '0 0 0 2px #fb0' : '0 0 2px 1px -moz-mac-focusring, inset 0 0 1px -moz-mac-focusring'; } function onBlur() { this.style.boxShadow = ''; } // determines whether value is valid number in attribute form function isAttrNum(value) { return !isNaN(value) && +value == parseFloat(value); } // validates min, max, and step attributes and redraws function update() { min = isAttrNum(slider.min) ? +slider.min : 0; max = isAttrNum(slider.max) ? +slider.max : 100; if (max < min) max = min > 100 ? min : 100; step = isAttrNum(slider.step) && slider.step > 0 ? +slider.step : 1; range = max - min; draw(true); } // recalculates value property function calc() { if (!isValueSet && !areAttrsSet) value = slider.getAttribute('value'); if (!isAttrNum(value)) value = (min + max) / 2;; // snap to step intervals (WebKit sometimes does not - bug?) value = Math.round((value - min) / step) * step + min; if (value < min) value = min; else if (value > max) value = min + ~~(range / step) * step; } // renders slider using CSS background ;) function draw(attrsModified) { calc(); if (isChanged && value != prevValue) slider.dispatchEvent(onChange); isChanged = false; if (!attrsModified && value == prevValue) return; prevValue = value; var position = range ? (value - min) / range * 100 : 0; var bg = '-moz-element(#__sliderthumb__) ' + position + '% no-repeat, '; style(slider, { background: bg + track }); } } function style(element, styles) { for (var prop in styles) element.style.setProperty(prop, styles[prop], 'important'); } })();