summaryrefslogtreecommitdiffstats
path: root/core/res/assets/webkit/youtube.html
blob: d808bcf7789b09f16fc439598c1522736e05d21c (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<html>
  <head>
    <style type="text/css">
      body      { background-color: black; }
      a:hover   { text-decoration: none; }
      a:link    { color: black; }
      a:visited { color: black; }
      #main {
        position: absolute;
        left: 0%;
        top: 0%;
        width: 100%;
        height: 100%;
        padding: 0%;
        z-index: 10;
      }
    </style>
  </head>
  <body id="body">
  <script type="text/javascript">
    // Nominal original size. If the embed is smaller than this, the play and logo
    // images get scaled appropriately. These are actually 3/4 of the sizes suggested
    // by youtube, so the images don't get too tiny.
    defHeight = 258;
    defWidth = 318;

    function setup() {
        var width = document.body.clientWidth;
        var height = document.body.clientHeight;
        var canvas = document.getElementById("canvas");
        // Resize the canvas to the right size
        canvas.width = width;
        canvas.height = height;
        var ctx = canvas.getContext('2d');
        var loadcount = 0;
        function doload() {
            if (++loadcount == 3) {
                // All images are loaded, so display them.
                // (Note that the images are loaded from javascript, so might load
                // after document.onload fires)

                playWidth = play.width;
                playHeight = play.height;
                logoWidth = logo.width;
                logoHeight = logo.height;
                var ratio = 1;
                // If the page is smaller than it 'should' be in either dimension
                // we scale the background, play button and logo according to the
                // dimension that has been shrunk the most.
                if (width / height > defWidth / defHeight && height < defHeight) {
                    ratio = height / defHeight;
                    // Stretch the background in this dimension only.
                    backgroundHeight = background.height / ratio;
                    ctx.drawImage(background, 0, 0, background.width, background.height,
                        0, (height - backgroundHeight) / 2, width, backgroundHeight);
                } else if (width / height < defWidth / defHeight && width < defWidth) {
                    ratio = width / defWidth;
                    backgroundWidth = background.width / ratio;
                    ctx.drawImage(background, 0, 0, background.width, background.height,
                        (width - backgroundWidth) / 2, 0, backgroundWidth, height);
                } else {
                    // In this case stretch the background in both dimensions to fill the space.
                    ctx.drawImage(background, 0, 0, width, height);
                }
                playWidth *= ratio;
                playHeight *= ratio;
                logoWidth *= ratio;
                logoHeight *= ratio;
                playLeft = (width - playWidth) / 2;
                playTop = (height - playHeight) / 2;
                ctx.drawImage(play, playLeft, playTop, playWidth, playHeight);
                ctx.globalAlpha = 0.7
                ctx.drawImage(logo, width - logoWidth, height - logoHeight, logoWidth, logoHeight);
                // To make it slightly easier to hit, the click target is twice the width/height of the unscaled play button
                targetLeft = width / 2 - play.width;
                targetRight = width / 2 + play.width;
                targetTop = height / 2 - play.height;
                targetBottom = height / 2 + play.height;

                canvas.addEventListener("click", function(e) {
                   var posx = e.clientX-canvas.offsetLeft;
                   var posy = e.clientY-canvas.offsetTop;
                   if (posx >= targetLeft && posx <= targetRight &&
                       posy >= targetTop && posy <= targetBottom) {
                       top.location.href = "vnd.youtube:VIDEO_ID";
                   }
               }, false);
            }
        }
        var background = new Image();
        background.onload = doload;
        background.src = "http://img.youtube.com/vi/VIDEO_ID/0.jpg";
        play = new Image();
        play.onload = doload;
        play.src = "play.png";
        logo = new Image();
        logo.onload = doload;
        logo.src = "youtube.png";
    }
    window.onload = setup;
  </script>
    <div id="main">
    <canvas id="canvas"></canvas>
    </div>
  </body>
</html>