Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3953|回复: 4

微信小程序

[复制链接]

399

主题

1251

帖子

4020

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4020
发表于 2023-12-7 18:19:22 | 显示全部楼层 |阅读模式
1.注册  参照https://www.zhihu.com/question/277664094
2.下载开发平台 https://developers.weixin.qq.com ... tools/download.html
3.编写代码  
4.调试
5.发布
回复

使用道具 举报

399

主题

1251

帖子

4020

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4020
 楼主| 发表于 2023-12-11 12:23:15 | 显示全部楼层
最终代码
  1. // index.js
  2. Page({
  3.   data: {
  4.     editText: 'concentration'
  5.   },
  6.   //const maxValue = 10;
  7.   inputChange(event) {
  8.     //console.log(event.detail.value) // 输出input的值
  9.     // 可以将获取的值传递给其他方法
  10.     var y = (-8.678*Math.log(event.detail.value)-44.860)/100+0.36768;
  11.     this.setData({
  12.       editText: Math.round(y).toString()+" mg/L",   
  13.     })

  14.     this.changeColor(y,event.detail.value)
  15.    
  16.   },
  17.   onReady: function () {
  18.     this.changeColor('#ff0000',"input current")
  19.   },
  20.   changeColor: function (value,text) {
  21.     wx.createSelectorQuery() .select('#myCanvas').fields({ node: true, size: true })
  22.     .exec((res) => {
  23.         // Canvas 对象
  24.         const canvas = res[0].node
  25.         console.log()
  26.         // Canvas 画布的实际绘制宽高
  27.         const renderWidth = res[0].width
  28.         const renderHeight = res[0].height
  29.         // Canvas 绘制上下文
  30.         const ctx = canvas.getContext('2d')

  31.         // 初始化画布大小
  32.         const dpr = wx.getWindowInfo().pixelRatio
  33.         canvas.width = renderWidth* dpr
  34.         canvas.height = renderHeight* dpr
  35.         ctx.scale(dpr, dpr)
  36.         const x = 50, y = 50, w = 200, h = 200, r = 10;
  37.         const BarNum = 50;
  38.         ctx.beginPath();
  39.         ctx.moveTo(x + r, y);
  40.         ctx.lineTo(x + w - r, y);
  41.         ctx.arcTo(x + w, y, x + w, y + r, r);
  42.         ctx.lineTo(x + w, y + h - r);
  43.         ctx.arcTo(x + w, y + h, x + w - r, y + h, r);
  44.         ctx.lineTo(x + r, y + h);
  45.         ctx.arcTo(x, y + h, x, y + h - r, r);
  46.         ctx.lineTo(x, y + r);
  47.         ctx.arcTo(x, y, x + r, y, r);
  48.         ctx.closePath();
  49.         ctx.fillStyle = this.getColorMap(value,0,10);
  50.         ctx.fill();
  51.         for(let i = 0; i < BarNum; i++){
  52.           this.drawColorBar(ctx,BarNum-i,Math.floor(h/BarNum),this.getColorMap(i,0,BarNum));
  53.         }   
  54.         //ctx.textAlign = "center";
  55.         //ctx.font="24px Verdana";
  56.         //ctx.fillStyle = "#FFFF00";
  57.         //ctx.fillText(text,150,160);
  58.     })
  59.    
  60.    
  61.   },
  62.   drawColorBar: function (ctx,i,h1,color) {   
  63.     const x1 = 260, y1 = 50, w1 = 20 ;
  64.     ctx.fillStyle = color;ctx.fillRect(x1,y1+i*h1,w1,h1);
  65.   },
  66.   getColorMap:function(colorNumber,startNum,EndNum){
  67.     const topColorR = 0x00,topColorG = 0x00,topColorB = 0xFF;
  68.     const bottomColorR = 0xFF ,bottomColorG =  0x00,bottomColorB =  0x00 ;
  69.     var ratio = (colorNumber-startNum)/(EndNum-startNum);
  70.     var R = Math.floor(topColorR+(bottomColorR-topColorR)*ratio);
  71.     var G =  Math.floor(topColorG+(bottomColorG-topColorG)*ratio);
  72.     var B =  Math.floor(topColorB+(bottomColorB-topColorB)*ratio);
  73.     return "#"+this.NumToString(R)+this.NumToString(G)+this.NumToString(B);
  74.   },
  75.   NumToString:function(num) {
  76.     var numlen = num.toString(16).length; //得到num的长度
  77.     var strChar = "0";  //空缺位填充字符
  78.     var str = num.toString(16);
  79.     //console.log(str)
  80.     for (var i = 0; i < 2 - numlen; i++) {
  81.         str = strChar + str;
  82.     }
  83.     return str;
  84. }
  85. })
复制代码
回复

使用道具 举报

399

主题

1251

帖子

4020

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4020
 楼主| 发表于 2023-12-11 12:23:22 | 显示全部楼层
  1. <!--index.wxml-->
  2. <navigation-bar title="colorSet" back="{{false}}" color="black" background="#FFF"></navigation-bar>
  3. <scroll-view class="scrollarea" scroll-y type="list">
  4.   <view class="container">
  5.    
  6.     <view  class ="classICCAS">CAAS-CAS</view>
  7.     <canvas id="myCanvas" height="800" type="2d"  class ="canvasClass" />
  8.     <view  class ="inputClass"><input id="myInput" placeholder="current" bindinput="inputChange" /></view>
  9.     <view  class ="inputClass1">10</view>
  10.     <view  class ="inputClass2">0</view>
  11.     <view  class ="inputClass3">{{editText}}</view>
  12.     <view  class ="classConcentration">Concentration (mg/L)</view>
  13.   </view>
  14. </scroll-view>
复制代码
回复

使用道具 举报

399

主题

1251

帖子

4020

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4020
 楼主| 发表于 2023-12-11 12:23:32 | 显示全部楼层
  1. /**index.wxss**/
  2. page {
  3.   height: 100vh;
  4.   display: flex;
  5.   flex-direction: column;
  6. }
  7. .scrollarea {
  8.   flex: 1;
  9.   overflow-y: hidden;
  10. }
  11. .canvasClass {
  12.   position: absolute;
  13.   top:6em;
  14.   left: 0em;
  15.   height: 20em;
  16.   color: white;
  17.   z-index: -1;
  18. }
  19. .inputClass{
  20.   position: absolute;
  21.   top:15em;
  22.   left: 3.8em;
  23.   color: yellow;
  24.   z-index:10;
  25.   text-align: center;
  26. }
  27. .inputClass1{
  28.   position: absolute;
  29.   top:8em;
  30.   left: 16em;
  31.   color: black;
  32. }
  33. .inputClass2{
  34.   position: absolute;
  35.   top:22em;
  36.   left: 16.5em;
  37.   color:  black;
  38. }
  39. .inputClass3{
  40.   position: absolute;

  41.   top:25em;
  42.   left: 8em;
  43.   color:  black;
  44. }
  45. .classConcentration{
  46.   position: absolute;
  47.   writing-mode:vertical-rl;
  48.   top:10em;
  49.   left: 17.5em;
  50.   color:  black;
  51. }
  52. .classICCAS{
  53.   position: absolute;
  54.   top:8em;
  55.   left: 7em;
  56.   color:  black;
  57. }
复制代码
回复

使用道具 举报

399

主题

1251

帖子

4020

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4020
 楼主| 发表于 2023-12-11 12:23:58 | 显示全部楼层

//index.json
{   
  "usingComponents": {
    "navigation-bar": "/components/navigation-bar/navigation-bar"
  },

  "renderer": "webview"  
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-6-8 09:47 , Processed in 0.039127 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表