crutilka.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div id="container">
  9. </div>
  10. <div id="container1">
  11. </div>
  12. <div id="container2">
  13. </div>
  14. <style>
  15. img{
  16. width: 200px;
  17. height: 200px;
  18. padding-right: 10px;
  19. }
  20. </style>
  21. <script>
  22. function Controll(el,{value=50,min=0,max=100,minAngel=0,maxAngel=360,step=1,wheelSpeed=0.1}={}){
  23. const img=document.createElement('img');
  24. img.src='http://c1.asmer.fe.a-level.com.ua/1@3x.png';
  25. el.append(img);
  26. const ratio=(maxAngel-minAngel) / (max-min);
  27. const getAngel=()=>(value-min)*ratio+minAngel;
  28. this.setValue=newValue=>{
  29. if(newValue>max){
  30. newValue=max;
  31. }
  32. if(newValue<min){
  33. newValue=min;
  34. }
  35. value=newValue;
  36. img.style.transform=`rotate(${getAngel()}deg)`;
  37. if(this.onchange && typeof this.onchange==='function'){
  38. this.onchange(value);
  39. }
  40. }
  41. img.onmousewheel=e=>{
  42. const {deltaY} = e;
  43. const newValue = value+deltaY*wheelSpeed;
  44. this.setValue(newValue);
  45. e.preventDefault();
  46. }
  47. img.onclick=e=>{
  48. console.log(e,img.width);
  49. const {layerX}=e;
  50. const {width} = img;
  51. if(layerX>width/2){
  52. this.setValue(value+step);
  53. }else{
  54. this.setValue(value-step);
  55. }
  56. }
  57. const toDeg = rad => ((rad*180)/Math.PI+360+90)%360;
  58. let prevMouseAngel=null;
  59. img.onmousedown=e=>{
  60. const y=e.layerY-img.height/2;
  61. const x=e.layerX-img.width/2;
  62. prevMouseAngel=toDeg(Math.atan2(y,x));
  63. console.log(x,y,getAngel());
  64. e.preventDefault();
  65. }
  66. img.onmousemove= e =>{
  67. if(prevMouseAngel===null) return;
  68. const y=e.layerY-img.height/2;
  69. const x=e.layerX-img.width/2;
  70. let currentAngel=toDeg(Math.atan2(y,x));
  71. let moveAngelDiff=(currentAngel-prevMouseAngel);
  72. this.setValue(value+moveAngelDiff/ratio);
  73. prevMouseAngel = currentAngel;
  74. }
  75. this.getValue=function(){
  76. return value;
  77. }
  78. img.onmouseout=img.onmouseup=()=>{
  79. prevMouseAngel=null;
  80. }
  81. this.setValue(value);
  82. }
  83. /*let volumeControl=new Controll(container,{value:50,min:0,max:100,minAngel:-90,maxAngel:90});
  84. volumeControl.onchange=value=>console.log(value);
  85. console.log(volumeControl.getValue());*/
  86. function setRGB(){
  87. document.body.style.background=`rgb(${red.getValue()},${blue.getValue()},${green.getValue()})`;
  88. }
  89. let audio=document.createElement('audio');
  90. container2.appendChild(audio);
  91. audio.src="Mania-Вьюга.ogg";
  92. audio.style.display=true;
  93. audio.volume=0.2;
  94. audio.controls=true;
  95. audio.autoplay=true;
  96. let time=audio.duration;
  97. audio.onended=function(){
  98. audio.remove();
  99. }
  100. function setVolume(){
  101. audio.volume=volumeControl.getValue();
  102. }
  103. function setSpeed(){
  104. audio.playbackRate=speedControl.getValue();
  105. }
  106. const red=new Controll(container,{min:0,max:255});
  107. red.onchange=setRGB;
  108. const green=new Controll(container,{min:0,max:255});
  109. green.onchange=setRGB;
  110. const blue=new Controll(container,{min:0,max:255});
  111. blue.onchange=setRGB;
  112. const volumeControl=new Controll(container1,{min:0,max:1,wheelSpeed:0.001,step:0.05});
  113. volumeControl.onchange=setVolume;
  114. const speedControl=new Controll(container1,{min:0.5,max:2,wheelSpeed:0.001,step:0.05});
  115. speedControl.onchange=setSpeed;
  116. </script>
  117. </body>
  118. </html>