Advertisement
dymos23

HTML5JSCanvas

Apr 17th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.01 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="cs" dir="ltr">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Titulek stránky</title>
  6. </head>
  7. <body onload="zobrazTelefon(248)">
  8.  
  9. <center>
  10. <form>
  11. <input id="velikost" type="range" min="0" max="496" onchange="zobrazTelefon(this.value)" style="width: 500px;">
  12. </form>
  13.  
  14.  
  15. <canvas id="myCanvas" width="500px" height="1000">
  16. </canvas>
  17. </center>
  18.  
  19. <script>
  20.  
  21. var c = document.getElementById("myCanvas");
  22. var ctx = c.getContext("2d");
  23.  
  24.  
  25.  
  26. function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
  27.     ctx.beginPath();
  28.     ctx.moveTo(x + radius, y);
  29.     ctx.lineTo(x + width - radius, y);
  30.     ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  31.     ctx.lineTo(x + width, y + height - radius);
  32.     ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  33.     ctx.lineTo(x + radius, y + height);
  34.     ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  35.     ctx.lineTo(x, y + radius);
  36.     ctx.quadraticCurveTo(x, y, x + radius, y);
  37.     ctx.closePath();
  38.     if (stroke) {
  39.         ctx.stroke();
  40.     }
  41.     if (fill) {
  42.         ctx.fill();
  43.     }        
  44. }
  45.  
  46. function circle(ctx, x, y, size, fill, stroke){
  47.     ctx.beginPath();
  48.     ctx.arc(x,y, size, 0, 2*Math.PI);
  49.     ctx.stroke();
  50.     if (stroke) {
  51.         ctx.stroke();
  52.     }
  53.     if (fill) {
  54.         ctx.fill();
  55.     }  
  56. }
  57.  
  58.  
  59. function zobrazTelefon(velikost){
  60.  
  61. c.height = c.height;
  62.  
  63. var posunX = (500-velikost)/2;
  64. var posunY = 10;
  65.  
  66. var TelefonSirka = velikost;
  67. var TelefonVyska = TelefonSirka * 1.96587030717;
  68. var TelefonZaobleni = TelefonSirka / 10;
  69. var TelefonTloustka = TelefonSirka / 100;
  70.  
  71. var DisplejSirka = TelefonSirka * 0.88054607508;
  72. var DisplejVyska = DisplejSirka * 1.48546511628;
  73. var DisplejOdsazeniZleva = (TelefonSirka - DisplejSirka) / 2;
  74. var DisplejOdsazeniZhora = (TelefonVyska - DisplejVyska) / 2;
  75.  
  76. var TlacitkoVelikost = TelefonSirka / 9;
  77. var TlacitkoOdsazeniZleva = TelefonSirka / 2;
  78. var TlacitkoOdsazeniZhora =  TelefonVyska - (DisplejOdsazeniZhora / 2);
  79. var TlacitkoTloustka = TelefonSirka / 100;
  80.  
  81. var TlacitkoSymbolVelikost = (TlacitkoVelikost*2)/3;
  82. var TlacitkoSymbolOdsazeniZleva = (TelefonSirka - TlacitkoSymbolVelikost) / 2;
  83. var TlacitkoSymbolOdsazeniZhora = TlacitkoOdsazeniZhora - (TlacitkoSymbolVelikost / 2);
  84. var TlacitkoSymbolTloustka = TelefonSirka / 150;
  85. var TlacitkoSymbolZaobleni = TlacitkoSymbolVelikost * 0.3375;
  86.  
  87. var TlacitkoGRD_zacatek = (TelefonSirka / 2) - TlacitkoVelikost;
  88. var TlacitkoGRD_konec = (TelefonSirka / 2) + TlacitkoVelikost;
  89. var grd = ctx.createLinearGradient(TlacitkoGRD_zacatek + posunX, 0, TlacitkoGRD_konec + posunX, 0);
  90. grd.addColorStop(0,"#2A2A2A");
  91. grd.addColorStop(1,"black");
  92.  
  93. var SluchatkoSirka = TelefonSirka / 4;
  94. var SluchatkoVyska = SluchatkoSirka / 8;
  95. var SluchatkoOdsazeniZleva = (TelefonSirka - SluchatkoSirka) / 2;
  96. var SluchatkoOdsazeniZhora = (DisplejOdsazeniZhora - SluchatkoVyska) / 2;
  97. var SluchatkoZaobleni = SluchatkoVyska / 2;
  98. var SluchatkoTloustka = SluchatkoVyska / 4;
  99.  
  100. var FotakPodkladVelikost = SluchatkoVyska / 2;
  101. var FotakPodkladOdsazeniZleva = TelefonSirka / 3.5;
  102. var FotakPodkladOdsazeniZhora = SluchatkoOdsazeniZhora + FotakPodkladVelikost;
  103.  
  104. var FotakVelikost = FotakPodkladVelikost / 2;
  105.  
  106. // telo telefonu
  107. roundRect(ctx, posunX, posunY, TelefonSirka-1+1, TelefonVyska, TelefonZaobleni, true, false);
  108.  
  109. // ramecek telefonu
  110. ctx.strokeStyle = '#D0D0D0';
  111. ctx.lineWidth = TelefonTloustka;
  112. roundRect(ctx, posunX, posunY, TelefonSirka-1+1, TelefonVyska, TelefonZaobleni, false, true);
  113.  
  114. // displej
  115. ctx.fillStyle = '#202020';
  116. ctx.fillRect(DisplejOdsazeniZleva + posunX, DisplejOdsazeniZhora + posunY, DisplejSirka, DisplejVyska);
  117.  
  118. // vypln tlacitka
  119. ctx.fillStyle = grd;
  120. //ctx.fillRect(0,0, 500, 50); // TEST PRECHODU
  121. circle(ctx, TlacitkoOdsazeniZleva + posunX, TlacitkoOdsazeniZhora + posunY, TlacitkoVelikost - (TlacitkoTloustka / 3), true, false);
  122.  
  123. // ramecek tlacitka
  124. ctx.lineWidth = TlacitkoTloustka;
  125. ctx.strokeStyle = '#181818';
  126. circle(ctx, TlacitkoOdsazeniZleva + posunX, TlacitkoOdsazeniZhora + posunY, TlacitkoVelikost, false, true);
  127.  
  128. // symbol tlacitka
  129. ctx.strokeStyle = '#D0D0D0';
  130. ctx.lineWidth = TlacitkoSymbolTloustka;
  131. roundRect(ctx, TlacitkoSymbolOdsazeniZleva + posunX, TlacitkoSymbolOdsazeniZhora + posunY, TlacitkoSymbolVelikost, TlacitkoSymbolVelikost, TlacitkoSymbolZaobleni, false, true);
  132.  
  133. // vypln sluchatka
  134. ctx.fillStyle = '#505050';
  135. roundRect(ctx, SluchatkoOdsazeniZleva + posunX, SluchatkoOdsazeniZhora + posunY, SluchatkoSirka, SluchatkoVyska, SluchatkoZaobleni, true, false);
  136.  
  137. // ramecek sluchatka
  138. ctx.strokeStyle = '#323232';
  139. ctx.lineWidth = SluchatkoTloustka;
  140. roundRect(ctx, SluchatkoOdsazeniZleva + posunX, SluchatkoOdsazeniZhora + posunY, SluchatkoSirka, SluchatkoVyska, SluchatkoZaobleni, false, true);
  141.  
  142. // podklad fotaku
  143. ctx.fillStyle = '#323232';
  144. circle(ctx, FotakPodkladOdsazeniZleva + posunX, FotakPodkladOdsazeniZhora + posunY, FotakPodkladVelikost, true, false);
  145.  
  146. // fotak
  147. ctx.fillStyle = '#151B54';
  148. circle(ctx, FotakPodkladOdsazeniZleva + posunX, FotakPodkladOdsazeniZhora + posunY, FotakVelikost, true, false);
  149.  
  150. }
  151.  
  152.  
  153.  
  154. </script>
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. </body>
  162. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement