slider_ruler.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {isObject,isString,isArray,isLawfulType,isFunction} from "@/common/js/tools.js"
  2. let config ={
  3. canvasid:{
  4. type: "string",
  5. required: true,
  6. validator: function (value) {
  7. return value.length>0
  8. },
  9. value:""
  10. },
  11. canvascursorid:{
  12. type: "string",
  13. required: true,
  14. validator: function (value) {
  15. return value.length>0
  16. },
  17. value:""
  18. },
  19. minvalue:{
  20. type: "number",
  21. required: true,
  22. validator: function (value) {
  23. return value>0
  24. },
  25. value:0
  26. },
  27. maxvalue:{
  28. type: "number",
  29. required: true,
  30. validator: function (value) {
  31. return value>0
  32. },
  33. value:0
  34. },
  35. interval:{
  36. type: "number",
  37. required: true,
  38. validator: function (value) {
  39. return value>0
  40. },
  41. value:0
  42. },
  43. defaultvalue:{
  44. type: "number",
  45. required: true,
  46. validator: function (value) {
  47. return value>0
  48. },
  49. value:0
  50. },
  51. precision:{
  52. //小数点位数
  53. type: "number",
  54. validator: function (value) {
  55. let values =[0,1,2]
  56. return values.indexOf(value)>=0;
  57. },
  58. value:0
  59. }
  60. }
  61. let screenWidth=0
  62. let screenHeight=0
  63. let heightDecimal=50
  64. let heightDigit=25
  65. let ratio=10
  66. let fontSize=20
  67. let canvasHeight=80
  68. let harfline=5
  69. let cursorheight=25
  70. let lineWidth1= 1
  71. let lineWidth2= 2
  72. let rulecolor1 ="#FFA500"
  73. let rulecolor2 ="#FF8C00"
  74. let cursorcolor = '#0000FF'
  75. let canvasWidth=0
  76. let value =0
  77. let scroll_left = 0
  78. function getvalue(key){
  79. // console.log(key,config[key].value)
  80. return typeof config[key].value != "undefined" ?config[key].value:false;
  81. }
  82. function setconfig(param){
  83. if(!isObject(param)){
  84. throw new Error("drawruler param error")
  85. }
  86. Object.entries(config).forEach(([k, v]) => {
  87. if(v.required){
  88. if(!param[k]){
  89. throw new Error("param "+k +" required")
  90. }
  91. }
  92. let value = param[k];
  93. if(v.type){
  94. if(!isLawfulType(v.type,value)){
  95. throw new Error("param "+k +" type error")
  96. }
  97. }
  98. if(isFunction(v.validator)){
  99. if(!v.validator(value)){
  100. throw new Error("param "+k +" value error")
  101. }
  102. }
  103. v.value = value
  104. config[k] = v
  105. })
  106. }
  107. function dodrawruler(){
  108. var context = uni.createCanvasContext(getvalue('canvasid'))
  109. let minvalue = getvalue('minvalue')
  110. let maxvalue = getvalue('maxvalue')
  111. let interval = getvalue('interval')
  112. let origionX = 0
  113. let len = (maxvalue - minvalue)*ratio
  114. origionX = screenWidth/2
  115. // if(len>=screenWidth){
  116. // origionX = screenWidth/2
  117. // }else{
  118. // origionX = (screenWidth-len)/2
  119. // }
  120. let origion = {x:origionX,y:0}
  121. let i = minvalue
  122. while(i <= maxvalue){
  123. context.beginPath()
  124. let movetoX=origion.x + (i - minvalue) * ratio
  125. let movetoY=0
  126. context.moveTo(movetoX, movetoY);
  127. let linetoX = origion.x + (i - minvalue) * ratio
  128. let linetoY = (i % ratio == 0 ? heightDecimal : heightDigit)
  129. context.lineTo(linetoX ,linetoY )
  130. context.setLineWidth(lineWidth2)
  131. context.setStrokeStyle(i % ratio == 0 ? rulecolor1 : rulecolor2)
  132. context.stroke()
  133. context.setFillStyle('gray')
  134. if (i % ratio == 0) {
  135. context.setFontSize(fontSize);
  136. context.fillText(i == 0 ? ' ' + i : i, origion.x + (i - minvalue) * ratio - fontSize / 2, heightDecimal + 5 + fontSize)
  137. }
  138. context.closePath()
  139. // context.draw(true)
  140. i+=interval
  141. }
  142. setTimeout(()=>{// uni-app必须加上延迟,不然显示不出来, 亲测
  143. context.draw()
  144. }, 50)
  145. }
  146. function setCanvasConfig(){
  147. uni.getSystemInfo({
  148. success: (res) => {
  149. screenWidth = res.windowWidth; // 屏幕宽度,单位为px
  150. screenHeight = res.windowHeight; // 屏幕高度,单位为px
  151. },
  152. });
  153. canvasWidth = (getvalue('maxvalue')- getvalue('minvalue')) * ratio + screenWidth;
  154. // canvasWidth =1000
  155. config['canvasWidth']={
  156. value:canvasWidth
  157. }
  158. config['canvasHeight']={
  159. value:canvasHeight
  160. }
  161. }
  162. function getCanvasConfig(){
  163. let use_config ={
  164. canvasWidth:getvalue('canvasWidth'),
  165. canvasHeight:getvalue('canvasHeight'),
  166. screenWidth:screenWidth,
  167. screenHeight:screenHeight
  168. }
  169. // console.log(use_config)
  170. return use_config
  171. }
  172. function drowncursor(){
  173. var context = uni.createCanvasContext(getvalue('canvascursorid'))
  174. var center = {x:screenWidth/2,y:0}
  175. console.log(center);
  176. var right ={x:center.x-harfline,y:0}
  177. var left ={x:center.x+harfline,y:0}
  178. context.moveTo(right.x, 0);
  179. context.lineTo(left.x, 0);
  180. context.lineTo(center.x, center.y+5);
  181. context.closePath();
  182. context.setFillStyle(cursorcolor)
  183. context.fill();
  184. context.beginPath()
  185. context.moveTo(center.x, 0);
  186. context.lineTo(center.x, 50);
  187. context.setLineWidth(lineWidth1)
  188. context.setStrokeStyle(cursorcolor)
  189. context.stroke();
  190. context.closePath();
  191. // context.draw(true)
  192. setTimeout(()=>{// uni-app必须加上延迟,不然显示不出来, 亲测
  193. context.draw()
  194. }, 50)
  195. let defaultvalue = getvalue('defaultvalue')
  196. if(defaultvalue>0){
  197. let minvalue = getvalue('minvalue')
  198. value = defaultvalue
  199. scroll_left = (value-minvalue)*10
  200. }else{
  201. let midvalue = (getvalue('maxvalue')-getvalue('minvalue'))/2
  202. value = midvalue+getvalue('minvalue')
  203. scroll_left = canvasWidth/2-screenWidth/2
  204. }
  205. }
  206. function getcursorvalue(){
  207. return value
  208. }
  209. function getscrollleft(){
  210. return scroll_left
  211. }
  212. function drawruler(param){
  213. setconfig(param)
  214. setCanvasConfig()
  215. dodrawruler()
  216. drowncursor()
  217. }
  218. function setcursorvalue(scrollLeft,minvalue=0){
  219. if(minvalue<=0){
  220. minvalue = getvalue('minvalue')
  221. }
  222. let precision = getvalue('precision')
  223. let value = 0
  224. if(precision==0){
  225. value = Math.round(scrollLeft/10)+minvalue;
  226. }else{
  227. value = parseFloat((scrollLeft/10).toFixed(precision))+minvalue
  228. }
  229. return value
  230. }
  231. export {drawruler,getCanvasConfig,getcursorvalue,setcursorvalue,getscrollleft}