summaryrefslogtreecommitdiff
path: root/public/dye.html
blob: 6e8026d879e9f6ac3b4b6096b1e817915c1ee8dc (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title>Dyer</title>
        <style type="text/css">
            .viewport {
                border:solid 1px #000000;
                display: inline-block;
                height: 400px;
                overflow: scroll;
                width: 600px;
            }
        </style>
        <script src="js/mp/dye.js"></script>
        <script src="js/mp/resource.js"></script>
    </head>
    <body>
        <div class="viewport" dropzone="link" ondrop="dyeDrop(event);" ondragover="event.preventDefault();">
            <canvas id="dyeCanvas"></canvas>
        </div>
        <div class="viewport" dropzone="link" ondrop="referenceDrop(event);" ondragover="event.preventDefault();"></div>
        
        <div style="display: none" >
            <canvas id="rawCanvas"></canvas>
        </div>
        
        <div><input type="text" id="dyeStringInput" size="100"/><button onclick="applyDye(event);">Apply</button></div>
        
        <script>
            var dyeCanvas = document.getElementById("dyeCanvas"),
                dyeStringInput = document.getElementById("dyeStringInput");
            
            var loadedImage = null;
            
            function applyDye(event) {
                if (loadedImage == null) {
                    return;
                }
                
                var ctx = dyeCanvas.getContext("2d");
                ctx.clearRect(0, 0, dyeCanvas.width, dyeCanvas.height);
                var clone = mp.resource.copyImageData(ctx, loadedImage);
                mp.dye.dyeImage(clone.data, mp.dye.parseDyeString(dyeStringInput.value));
                ctx.putImageData(clone, 0, 0);
            }
            
            function dyeDrop(event) {
                event.stopPropagation();
                event.preventDefault();
                var file = event.dataTransfer.files[0],
                    reader = new FileReader();
                reader.onload = function (event) {
                    mp.resource.loadImage(event.target.result, function(err, imageData) {
                        if (!err) {
                            loadedImage = imageData;
                            dyeCanvas.width = loadedImage.width;
                            dyeCanvas.height = loadedImage.height;
                            applyDye(null);
                        }
                    });
                };
                reader.readAsDataURL(file);
                
                return false;
            }
            
            function referenceDrop(event) {
                event.stopPropagation();
                event.preventDefault();
                var file = event.dataTransfer.files[0],
                    reader = new FileReader();
                reader.onload = function (event) {
                    var img = new Image();
                    img.src = event.target.result;
                    referenceViewport.innerHTML = '';
                    referenceViewport.appendChild(img);
                };
                reader.readAsDataURL(file);
                return false;
            }
        </script>
    </body>
</html>