Path: blob/master/6-Selenium/phantomjs/examples/colorwheel.js
164 views
var page = require('webpage').create();1page.viewportSize = { width: 400, height : 400 };2page.content = '<html><body><canvas id="surface"></canvas></body></html>';3page.evaluate(function() {4var el = document.getElementById('surface'),5context = el.getContext('2d'),6width = window.innerWidth,7height = window.innerHeight,8cx = width / 2,9cy = height / 2,10radius = width / 2.3,11imageData,12pixels,13hue, sat, value,14i = 0, x, y, rx, ry, d,15f, g, p, u, v, w, rgb;1617el.width = width;18el.height = height;19imageData = context.createImageData(width, height);20pixels = imageData.data;2122for (y = 0; y < height; y = y + 1) {23for (x = 0; x < width; x = x + 1, i = i + 4) {24rx = x - cx;25ry = y - cy;26d = rx * rx + ry * ry;27if (d < radius * radius) {28hue = 6 * (Math.atan2(ry, rx) + Math.PI) / (2 * Math.PI);29sat = Math.sqrt(d) / radius;30g = Math.floor(hue);31f = hue - g;32u = 255 * (1 - sat);33v = 255 * (1 - sat * f);34w = 255 * (1 - sat * (1 - f));35pixels[i] = [255, v, u, u, w, 255, 255][g];36pixels[i + 1] = [w, 255, 255, v, u, u, w][g];37pixels[i + 2] = [u, u, w, 255, 255, v, u][g];38pixels[i + 3] = 255;39}40}41}4243context.putImageData(imageData, 0, 0);44document.body.style.backgroundColor = 'white';45document.body.style.margin = '0px';46});4748page.render('colorwheel.png');4950phantom.exit();515253