Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>Koch Curve</title>
- <style type="text/css">
- body {
- background: #000;
- height: 100%;
- overflow: hidden;
- margin:0;
- padding: 0;
- }
- #old-navigator {
- position: absolute;
- top: 50%;
- margin-top: -0.5em;
- color: #F78FE5;
- font: 24px sans-serif;
- text-align: center;
- width: 100%;
- }
- .x-line {
- position: absolute;
- height: 2px;
- background: #FFF;
- -webkit-transform-origin: 0 100%;
- -o-transform-origin: 0 100%;
- -moz-transform-origin: 0 100%;
- -ms-transform-origin: 0 100%;
- transform-origin: 0 100%;
- }
- </style>
- <script type="text/javascript">
- function Line(ax, ay, bx, by) {
- this.ax = ax;
- this.ay = ay;
- this.bx = bx;
- this.by = by;
- }
- Line.prototype = {
- transformProperty: function getTransformProperty() {
- if (!document.body) {
- document.addEventListener("DOMContentLoaded", function () {
- Line.prototype.transformProperty = getTransformProperty();
- });
- return;
- }
- var style = document.body.style, properties = {
- 'transform': 'transform',
- 'mozTransform': '-moz-transform',
- 'oTransform': '-o-transform',
- 'msTransform': '-ms-transform',
- 'webkitTransform': '-webkit-transform'
- }, property;
- for (property in properties) {
- if (property in style) {
- return properties[property];
- }
- }
- throw new Error('WTF?');
- }(),
- getA: function () {
- return {
- x: this.ax,
- y: this.ay
- };
- },
- getB: function () {
- return {
- x: this.bx,
- y: this.by
- };
- },
- getLength: function () {
- var ax = this.ax, ay = this.ay, bx = this.bx, by = this.by;
- return Math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay));
- },
- getAngle: function () {
- var ax = this.ax, ay = this.ay, bx = this.bx, by = this.by;
- return Math.atan2(by - ay, bx - ax);
- },
- getDegAngle: function () {
- return this.getAngle() * 180 / Math.PI;
- },
- create: function () {
- var result = document.createElement('div');
- result.className = 'x-line';
- result.style.width = this.getLength() + 'px';
- result.style.left = this.ax + 'px';
- result.style.top = this.ay + 'px';
- result.style[this.transformProperty ] = 'rotate(' + this.getDegAngle() +'deg)';
- return result;
- }
- };
- function byte2Hex(n) {
- var nybHexString = "0123456789ABCDEF";
- return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
- }
- function RGB2Color(r,g,b) {
- return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
- }
- function makeColorGradient(len, redPhase, greenPhase, bluePhase) {
- var amplitude = 128, center = 127,
- i, step = 2*Math.PI/len, colors = [],
- red, green, blue;
- redPhase = redPhase || 0;
- greenPhase = greenPhase || Math.PI/2;
- bluePhase = bluePhase || 4*Math.PI/3;
- for (var i = 0; i < len; i++) {
- red = Math.sin(step*i + redPhase)*amplitude + center;
- green = Math.sin(step*i + greenPhase)*amplitude + center;
- blue = Math.sin(step*i + bluePhase)*amplitude + center;
- colors.push(RGB2Color(red, green, blue));
- }
- return colors;
- }
- function drawCurve(ax, ay, bx, by, depth, container) {
- var colors = makeColorGradient(100),
- linesAmount = Math.pow(4, depth),
- linesPerColor = linesAmount / colors.length,
- currentLine = 0,
- currentInterval = 0;
- function nexColor() {
- if (currentLine > currentInterval * linesPerColor) {
- currentInterval++;
- }
- return colors[Math.floor(++currentLine / linesPerColor)];
- }
- function draw(line, iter) {
- if (!iter) {
- var line = container.appendChild(line.create());
- line.style.backgroundColor = nexColor();
- container.appendChild(line);
- } else {
- var part = line.getLength() / 3,
- bias = line.getAngle(),
- ax = line.ax, ay = line.ay, bx = line.bx, by = line.by,
- a, b, c, d;
- a = new Line(ax, ay, ax + Math.cos(bias) * part, ay + Math.sin(bias) * part);
- b = new Line(a.bx, a.by, a.bx + Math.cos(bias + Math.PI/3) * part, a.by + Math.sin(bias + Math.PI/3) * part);
- c = new Line(b.bx, b.by, ax + Math.cos(bias) * part * 2, ay + Math.sin(bias) * part * 2);
- d = new Line(c.bx, c.by, bx, by);
- draw(a, iter - 1);
- draw(b, iter - 1);
- draw(c, iter - 1);
- draw(d, iter - 1);
- }
- }
- draw(new Line(ax, ay, bx, by), depth);
- }
- function launch () {
- var body = document.body;
- body.removeChild(document.getElementById('old-navigator'));
- /*body.appendChild((new Line(10, 5, 100, 5)).create());
- body.appendChild((new Line(100, 5, 50, 100)).create());
- body.appendChild((new Line(10, 5, 50, 100)).create());*/
- drawCurve(0, 0, window.innerWidth, 0, 5, body);
- }
- document.addEventListener("DOMContentLoaded", launch, false);
- </script>
- </head>
- <body>
- <div id="old-navigator">
- Upgrade Your Browser! ;]
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment