-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringtoimage.js
69 lines (60 loc) · 2.15 KB
/
stringtoimage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Dependency jQuery.
$(function() {
var pallete = [
["#FFAAAA", "#D46A6A"],
["#774A8E", "#9874AA"],
["#88CC88", "#55AA55"],
["#8080B3", "#565695"]
];
var seedFromString = function(seed, test_name, options) {
word = CryptoJS.MD5("" + seed + test_name).words[0];
i = Math.abs(word);
return i;
}
/*
* Function to create a new random pattern and return the dataURI.
* Accepts: number - acts as a seed.
* Returns: String - DataURI
*/
function createPattern(seed) {
var height = 25 + seed % 25,
heightBy4 = height / 4,
theCanvas = $("<canvas width='" + height + "' height='" + height + "'>")[0],
theContext = theCanvas.getContext("2d"),
palleteItem = pallete[seed % pallete.length];
if (seed % 2) {
// Filled triangle
theContext.beginPath();
theContext.moveTo(0, 0);
theContext.lineTo(height, 0);
theContext.lineTo(0, height);
theContext.fillStyle = palleteItem[0];
theContext.fill();
theContext.beginPath();
theContext.moveTo(height, height);
theContext.lineTo(height, 0);
theContext.lineTo(0, height);
theContext.fillStyle = palleteItem[1];
theContext.fill();
} else {
//Circle Mode.
theContext.arc(heightBy4, heightBy4, heightBy4, 0, Math.PI * 2);
theContext.fillStyle = palleteItem[1];
theContext.fill();
theContext.beginPath();
theContext.arc(height - height / 4, height - height / 4, height / 4, 0, Math.PI * 2, false);
theContext.fillStyle = palleteItem[0];
theContext.fill();
}
return theCanvas.toDataURL();
}
function assignRandomBG(seed) {
$("body").css('background-image', 'url(' + createPattern(seed) + ')');
}
$("#thebox").on("keyup", function() {
var text = $(this).val();
var rand = seedFromString(text);
assignRandomBG(rand);
});
assignRandomBG(seedFromString($("#thebox").val()));
});