script.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. function Control(el, {value=50,
  2. min=40,
  3. max=100,
  4. minAngle=90,
  5. maxAngle=360,
  6. wheelSpeed=0.1,
  7. step=1}={}){
  8. const img = document.createElement('img')
  9. img.src = '1@3x.png'
  10. el.append(img)
  11. this.getValue = () => value;
  12. const ratio = (maxAngle - minAngle) / (max - min)
  13. const getAngle = () => (value - min) * ratio + minAngle
  14. this.setValue = newValue => {
  15. if (newValue > max){
  16. newValue = max
  17. }
  18. if (newValue < min){
  19. newValue = min
  20. }
  21. value = newValue
  22. if ("onchange" in this && typeof this.onchange === "function"){
  23. this.onchange(value);
  24. }
  25. img.style.transform = `rotate(${getAngle()}deg)`;
  26. }
  27. img.onmousewheel = e => {
  28. const {deltaY} = e
  29. const newValue = value + deltaY*wheelSpeed
  30. this.setValue(newValue)
  31. e.preventDefault()
  32. }
  33. img.onclick = e => {
  34. console.log(e, img.width)
  35. const {layerX} = e
  36. const {width} = img
  37. if (layerX > width/2){
  38. this.setValue(value +step)
  39. }
  40. else {
  41. this.setValue(value -step)
  42. }
  43. //найти координаты относительно img
  44. //найти, левее или правее центра происходит клик
  45. //если правее - сделать setValue(value +step)
  46. //если левее - сделать setValue(value -step)
  47. }
  48. const toDeg = rad => ((rad * 180)/Math.PI + 360 + 90) % 360
  49. let prevMouseAngle = null
  50. img.onmousedown = e => {
  51. const y = e.layerY - img.height/2
  52. const x = e.layerX - img.width/2
  53. prevMouseAngle = toDeg(Math.atan2(y, x))
  54. // console.log(x,y, getAngle(), prevMouseAngle, prevMouseAngle - getAngle())
  55. e.preventDefault()
  56. }
  57. img.onmousemove = e => {
  58. if (prevMouseAngle === null) return
  59. const y = e.layerY - img.height/2
  60. const x = e.layerX - img.width/2
  61. let currentAngle = toDeg(Math.atan2(y, x))
  62. let moveAngleDiff = (currentAngle - prevMouseAngle)
  63. this.setValue(value + moveAngleDiff/ratio)
  64. prevMouseAngle = currentAngle
  65. }
  66. img.onmouseout = img.onmouseup = () => {
  67. prevMouseAngle = null
  68. }
  69. this.setValue(value);
  70. }
  71. const volumeControl = new Control(container1, {value: 50})
  72. volumeControl.onchange = value => console.log(value)
  73. console.log(volumeControl.getValue());
  74. // RGB
  75. let test = document.querySelector('.test');
  76. function setRGB (){
  77. test.style.backgroundColor = 'rgb(' + red.getValue() + ',' + green.getValue() + ',' + blue.getValue() + ')';
  78. }
  79. const red = new Control(container2, {min: 0, max: 255})
  80. red.onchange = setRGB;
  81. const green = new Control(container2, {min: 0, max: 255})
  82. green.onchange = setRGB;
  83. const blue = new Control(container2, {min: 0, max: 255})
  84. blue.onchange = setRGB;
  85. // Music
  86. let blockMusic = document.querySelector('#myPlayer');
  87. const music = new Control(container3, {min: 0, max: 100})
  88. music.onchange = (value) => {blockMusic.volume = value / 100};
  89. //Password
  90. function Password(parent, open){
  91. let value = '';
  92. let passWordForm = document.createElement('input');
  93. let buttonForm = document.createElement('button');
  94. buttonForm.textContent = 'invisible';
  95. passWordForm.placeholder = 'password';
  96. parent.append(passWordForm);
  97. parent.append(buttonForm);
  98. buttonForm.onclick = () => {
  99. if ("onOpenChange" in this && typeof this.onOpenChange === "function"){
  100. this.onOpenChange(!!open);
  101. }
  102. if (open === true) {
  103. buttonForm.textContent = 'visible';
  104. open = false;
  105. let hidden = '';
  106. for (let i = 0; i < value.length; i++) {
  107. hidden += '*';
  108. }
  109. passWordForm.value = hidden;
  110. }
  111. else if (open === false) {
  112. buttonForm.textContent = 'invisible';
  113. open = true;
  114. passWordForm.value = value;
  115. }
  116. }
  117. passWordForm.oninput = () => {
  118. value = passWordForm.value;
  119. if ("onChange" in this && typeof this.onChange === "function"){
  120. this.onChange(value);
  121. }
  122. }
  123. this.setValue = newValue => {
  124. if (newValue.length > 4 && newValue.length < 20){
  125. value = newValue;
  126. passWordForm.value = value;
  127. }
  128. }
  129. this.getValue = () => value;
  130. this.setOpen = bool => {
  131. if (typeof bool === 'boolean') {
  132. open = bool;
  133. buttonForm.onclick;
  134. }
  135. }
  136. this.getOpen = () => !!open;
  137. }
  138. // let p = new Password(document.body, true)
  139. // p.onChange = data => console.log(data)
  140. // p.onOpenChange = open => console.log(open)
  141. // p.setValue('qwerty')
  142. // console.log(p.getValue())
  143. // p.setOpen(false)
  144. // console.log(p.getOpen())
  145. // LoginForm
  146. function LoginForm () {
  147. let login = document.createElement('input');
  148. login.placeholder = 'login';
  149. login.oninput = () => disable();
  150. let pass = new Password(containerForm, true);
  151. pass.setValue = pass.value;
  152. pass.onChange = () => disable();
  153. let btn = document.createElement('button');
  154. btn.disabled = true;
  155. btn.textContent = 'Submit';
  156. containerForm.append(login);
  157. containerForm.append(btn);
  158. function disable(data) {
  159. btn.disabled = !(pass.getValue() !== '' && login.value !== '');
  160. }
  161. }
  162. let loginForm = new LoginForm();
  163. //LoginForm Constructor
  164. function LoginFormConstructor() {
  165. let login = document.createElement('input');
  166. login.placeholder = 'login';
  167. login.oninput = () => disable();
  168. let pass = new Password(containerForm, true);
  169. pass.setValue = pass.value;
  170. pass.onChange = () => disable();
  171. let btn = document.createElement('button');
  172. btn.disabled = true;
  173. btn.textContent = 'Submit';
  174. containerForm.append(login);
  175. containerForm.append(btn);
  176. function disable(data) {
  177. btn.disabled = !(pass.getValue() !== '' && login.value !== '');
  178. }
  179. this.getValueLogin = () => login.value;
  180. this.setValueLogin = newValue => {
  181. if (newValue.length > 4 && newValue.length < 20){
  182. login.value = newValue;
  183. }
  184. };
  185. this.getValuePassword = () => pass.getValue();
  186. this.setValuePassword = newValue => pass.setValue(newValue);
  187. this.getOpenPassword = () => pass.getOpen();
  188. this.setOpenPassword = bool => pass.setOpen(bool);
  189. }
  190. //Password Verify
  191. let pass1 = new Password(containerForm1, true);
  192. let pass2 = new Password(containerForm1, true);
  193. let btn = document.createElement('button');
  194. btn.textContent = 'Checked';
  195. btn.disabled = true;
  196. containerForm1.append(btn);
  197. let secondPass = document.createElement('input');
  198. secondPass.type = 'password';
  199. secondPass.style.visibility = 'hidden';
  200. containerForm1.append(secondPass);
  201. function check(){
  202. btn.disabled = pass1.getValue() !== pass2.getValue();
  203. }
  204. btn.onclick = () => {
  205. if (btn.disabled === false) {
  206. if (pass1.getOpen() === false && pass2.getOpen() === false) {
  207. secondPass.value = pass1.getValue();
  208. secondPass.style.visibility = 'visible';
  209. }
  210. else if(pass1.getOpen() === true && pass2.getOpen() === true) {
  211. secondPass.value = pass1.getValue();
  212. secondPass.style.visibility = 'hidden';
  213. }
  214. }
  215. }
  216. pass1.onChange = () => check();
  217. pass2.onChange = () => check();