• uni-app:实现页面效果2(canvas绘制,根据页面宽度调整元素位置)


    效果

     

    代码

    1. <template>
    2. <view>
    3. <view class="content_position">
    4. <view class="content">
    5. <view class="SN">
    6. <view class="SN_title">设备1view>
    7. <view class="SN_input">
    8. SN:
    9. <input type="text">
    10. view>
    11. view>
    12. <view class="line1">
    13. <view class="line1_item3">
    14. <view class="item3_top">
    15. <view class="item3_top_title_position">
    16. <view class="item3_top_title">
    17. <view>CO/VIview>
    18. view>
    19. view>
    20. <view class="item3_top_canvas_position">
    21. <canvas style="width:100%; height: 80px;" canvas-id="firstCanvas"
    22. id="firstCanvas">canvas>
    23. view>
    24. <view class="item3_top_text_position">
    25. <view class="top_text1">
    26. <view>
    27. <view class="text_center">CO浓度值view>
    28. <view class="circle">
    29. <view>
    30. <view class="text_center">
    31. 0
    32. view>
    33. <view class="text_center">
    34. PPM
    35. view>
    36. view>
    37. view>
    38. view>
    39. view>
    40. <view class="top_text2">
    41. <view>
    42. <view class="text_center">VI可见度view>
    43. <view class="circle">
    44. <view>
    45. <view class="text_center">
    46. 0
    47. view>
    48. <view class="text_center">
    49. 0M
    50. view>
    51. view>
    52. view>
    53. view>
    54. view>
    55. view>
    56. view>
    57. <view class="item3_bottom">
    58. <view class="line1_item3_block2">
    59. <view class="block2">
    60. <view class="title">
    61. CO信号输入
    62. view>
    63. <view class="green_ring_select">
    64. <view>DI7view>
    65. <view>
    66. <image :src="down" mode="">image>
    67. view>
    68. view>
    69. <view class="block2_input">
    70. <view class="block2_input_title">
    71. 量程
    72. view>
    73. <view class="block2_input_data">
    74. <input type="text" value="0PPM">
    75. <input type="text" value="300PPM">
    76. view>
    77. view>
    78. view>
    79. view>
    80. <view class="line1_item3_block2">
    81. <view class="block2">
    82. <view class="title">
    83. CO信号输入
    84. view>
    85. <view class="green_ring_select">
    86. <view>DI7view>
    87. <view>
    88. <image :src="down" mode="">image>
    89. view>
    90. view>
    91. <view class="block2_input">
    92. <view class="block2_input_title">
    93. 量程
    94. view>
    95. <view class="block2_input_data">
    96. <input type="text" value="0M">
    97. <input type="text" value="5000M">
    98. view>
    99. view>
    100. view>
    101. view>
    102. view>
    103. view>
    104. view>
    105. view>
    106. view>
    107. view>
    108. template>
    109. <script>
    110. export default {
    111. data() {
    112. return {
    113. down: getApp().globalData.icon + 'look/down.png',
    114. }
    115. },
    116. onReady: function(e) {
    117. //创建一个画布上下文对象,用于进行绘图操作。'firstCanvas'是一个指定的画布标识符,表示在页面上的哪个 元素上进行绘制。
    118. var context = uni.createCanvasContext('firstCanvas')
    119. var width = '';
    120. const query = uni.createSelectorQuery();
    121. //获取宽度
    122. query.select('#firstCanvas').fields({
    123. size: true
    124. }, (res) => {
    125. width = res.width;
    126. }).exec();
    127. //绘制路径中的线条。
    128. context.setStrokeStyle("#aaaaff")
    129. // 设置线条的宽度为2个像素。
    130. context.setLineWidth(2)
    131. // 绘制横线
    132. context.beginPath(); // 开始路径绘制
    133. context.moveTo(width / 2, 0); // 将起点移动到 (0, 100)
    134. context.lineTo(width / 2, 50);
    135. context.stroke(); // 绘制线条
    136. var x1 = width / 4; // 第一个竖线的起点 x 坐标
    137. var y1 = 50; // 第一个竖线的起点 y 坐标
    138. var y2 = 30; // 短竖线的高度
    139. var horizontalLength = width / 2; // 横线的长度
    140. context.beginPath();
    141. context.moveTo(x1, y1 + y2); // 移动到第一个短竖线的起点
    142. context.lineTo(x1, y1); // 绘制第一个短竖线
    143. context.moveTo(x1 + horizontalLength, y1 + y2); // 移动到横线的右端下方
    144. context.lineTo(x1 + horizontalLength, y1); // 绘制第二个短竖线
    145. context.moveTo(x1, y1); // 移动到横线的左端下方
    146. context.lineTo(x1 + horizontalLength, y1); // 绘制横线/* */
    147. context.stroke(); // 绘制线条
    148. // 将之前的绘图操作渲染到画布上。
    149. context.draw()
    150. // 获取父元素的宽度
    151. var parentWidth = document.querySelector('.item3_top_text_position').offsetWidth;
    152. console.log(parentWidth);
    153. // 计算margin-left的值
    154. var marginLeft = parentWidth / 4;
    155. // 设置样式
    156. document.querySelector('.top_text2').style.marginLeft = marginLeft + 'px';
    157. document.querySelector('.top_text1').style.marginLeft = '-' + marginLeft + 'px';
    158. },
    159. methods: {
    160. }
    161. }
    162. script>
    163. <style lang="scss">
    164. page {
    165. background-color: #f3f4f6;
    166. }
    167. .text_center {
    168. display: flex;
    169. justify-content: center;
    170. }
    171. /* 总体样式 */
    172. .content_position {
    173. width: 100%;
    174. display: flex;
    175. align-items: center;
    176. justify-content: center;
    177. }
    178. .content {
    179. width: 90%;
    180. padding: 2% 0;
    181. }
    182. /* SN 样式 */
    183. .SN {
    184. color: #6b6c6e;
    185. padding: 2%;
    186. display: flex;
    187. // border:1px solid black;
    188. justify-content: space-between;
    189. /* 将左右视图分散对齐 */
    190. .SN_input {
    191. display: flex;
    192. // border:1px solid black;
    193. input {
    194. border-bottom: 1px solid #a3a4a6;
    195. width: 200rpx;
    196. }
    197. image {
    198. width: 48rpx;
    199. height: 48rpx;
    200. }
    201. }
    202. }
    203. // 行1样式
    204. .line1 {
    205. // border: 1px solid black;
    206. background-color: #fff;
    207. border-radius: 15px;
    208. margin: 3% 0;
    209. padding: 3% 0;
    210. }
    211. .line1_item3 {
    212. // 顶部样式
    213. .item3_top {
    214. // position:relative;s
    215. }
    216. //第一行标题
    217. //标题位置
    218. .item3_top_title_position {
    219. // border: 1px solid black;
    220. display: flex;
    221. align-items: center;
    222. justify-content: center;
    223. }
    224. //标题内容
    225. .item3_top_title {
    226. width: 160rpx;
    227. height: 160rpx;
    228. border-radius: 50%;
    229. background-color: #edf1fc;
    230. color: #8196ec;
    231. font-size: 120%;
    232. font-weight: bold;
    233. display: flex;
    234. align-items: center;
    235. justify-content: center;
    236. // border:1px solid black
    237. }
    238. //画布位置
    239. .item3_top_canvas_position {
    240. // border: 1px solid black;
    241. display: flex;
    242. align-items: center;
    243. justify-content: center;
    244. }
    245. //文本位置
    246. .item3_top_text_position {
    247. // border: 1px solid black;
    248. position: relative;
    249. height: 400rpx;
    250. }
    251. .top_text1 {
    252. position: absolute;
    253. left: 50%;
    254. transform: translateX(-50%);
    255. margin-left: -85px;
    256. // border: 1px solid black;
    257. width: 220rpx;
    258. height: 350rpx;
    259. background-color: #edf1fc;
    260. border-radius: 10px;
    261. display: flex;
    262. align-items: center;
    263. justify-content: center;
    264. }
    265. .top_text2 {
    266. position: absolute;
    267. left: 50%;
    268. transform: translateX(-50%);
    269. margin-left: 85px;
    270. // border: 1px solid black;
    271. width: 220rpx;
    272. height: 350rpx;
    273. background-color: #edf1fc;
    274. border-radius: 10px;
    275. display: flex;
    276. align-items: center;
    277. justify-content: center;
    278. }
    279. .circle {
    280. width: 140rpx;
    281. height: 140rpx;
    282. border-radius: 50%;
    283. font-size: 95%;
    284. display: flex;
    285. align-items: center;
    286. justify-content: center;
    287. background-color: #e8e8e8;
    288. }
    289. // 底部样式
    290. .item3_bottom {
    291. display: flex;
    292. justify-content: space-between; //分散排列
    293. // border: 1px solid black;
    294. }
    295. //两列
    296. .line1_item3_block2 {
    297. display: flex;
    298. text-align: center;
    299. margin: 0 5%; //增加底部两个模块之间的距离
    300. }
    301. //两个小块样式
    302. .block2_input {
    303. margin: 5% 0;
    304. display: flex;
    305. }
    306. // 底部两小块标题
    307. .block2_input_title {
    308. width: 30%;
    309. display: flex;
    310. align-items: center; //竖直居中
    311. justify-content: center; //水平居中
    312. // border: 1px solid black;
    313. }
    314. .block2_input_data {
    315. width: 70%;
    316. }
    317. .block2_input_data input {
    318. margin: 5% 0;
    319. border: 1px solid #808080;
    320. border-radius: 20px;
    321. padding: 2% 5%;
    322. color: #737373;
    323. text-align: right;
    324. }
    325. //绿环样式
    326. .green_ring_select {
    327. background-color: #6da54f;
    328. border-radius: 20px;
    329. color: #fff;
    330. font-size: 95%;
    331. display: flex;
    332. align-items: center;
    333. padding: 1% 0;
    334. margin: 5% 0;
    335. // width:200rpx;
    336. view:nth-child(1) {
    337. width: 60%;
    338. display: flex;
    339. justify-content: center;
    340. align-items: center;
    341. }
    342. view:nth-child(2) {
    343. width: 40%;
    344. display: flex;
    345. justify-content: center;
    346. align-items: center;
    347. image {
    348. width: 30rpx;
    349. height: 30rpx;
    350. }
    351. }
    352. }
    353. }
    354. style>

    上面的额问题或许在手机端执行会出现问题 ,修改后的代码

    1. <template>
    2. <view>
    3. <view class="content_position">
    4. <view class="content">
    5. <view class="SN">
    6. <view class="SN_title">设备1view>
    7. <view class="SN_input">
    8. SN:
    9. <input type="text">
    10. view>
    11. view>
    12. <view class="line1">
    13. <view class="line1_item3">
    14. <view class="item3_top">
    15. <view class="item3_top_title_position">
    16. <view class="item3_top_title">
    17. <view>CO/VIview>
    18. view>
    19. view>
    20. <view class="item3_top_canvas_position">
    21. <canvas style="width:100%; height: 80px;" canvas-id="firstCanvas"
    22. id="firstCanvas">canvas>
    23. view>
    24. <view class="item3_top_text_position">
    25. <view class="top_text1" ref="myView1" :style="{ marginLeft: marginLeft1 + 'px' }">
    26. <view>
    27. <view class="text_center">CO浓度值view>
    28. <view class="circle">
    29. <view>
    30. <view class="text_center">
    31. 0
    32. view>
    33. <view class="text_center">
    34. PPM
    35. view>
    36. view>
    37. view>
    38. view>
    39. view>
    40. <view class="top_text2" ref="myView2" :style="{ marginLeft: marginLeft2 + 'px'}">
    41. <view>
    42. <view class="text_center">VI可见度view>
    43. <view class="circle">
    44. <view>
    45. <view class="text_center">
    46. 0
    47. view>
    48. <view class="text_center">
    49. 0M
    50. view>
    51. view>
    52. view>
    53. view>
    54. view>
    55. view>
    56. view>
    57. <view class="item3_bottom">
    58. <view class="line1_item3_block2">
    59. <view class="block2">
    60. <view class="title">
    61. CO信号输入
    62. view>
    63. <view class="green_ring_select">
    64. <view>DI7view>
    65. <view>
    66. <image :src="down" mode="">image>
    67. view>
    68. view>
    69. <view class="block2_input">
    70. <view class="block2_input_title">
    71. 量程
    72. view>
    73. <view class="block2_input_data">
    74. <input type="text" value="0PPM">
    75. <input type="text" value="300PPM">
    76. view>
    77. view>
    78. view>
    79. view>
    80. <view class="line1_item3_block2">
    81. <view class="block2">
    82. <view class="title">
    83. CO信号输入
    84. view>
    85. <view class="green_ring_select">
    86. <view>DI7view>
    87. <view>
    88. <image :src="down" mode="">image>
    89. view>
    90. view>
    91. <view class="block2_input">
    92. <view class="block2_input_title">
    93. 量程
    94. view>
    95. <view class="block2_input_data">
    96. <input type="text" value="0M">
    97. <input type="text" value="5000M">
    98. view>
    99. view>
    100. view>
    101. view>
    102. view>
    103. view>
    104. view>
    105. view>
    106. view>
    107. view>
    108. template>
    109. <script>
    110. export default {
    111. data() {
    112. return {
    113. down: getApp().globalData.icon + 'look/down.png',
    114. marginLeft1: 0, // 子元素的外边距
    115. marginLeft2: 0, // 子元素的外边距
    116. }
    117. },
    118. onReady: function(e) {
    119. //创建一个画布上下文对象,用于进行绘图操作。'firstCanvas'是一个指定的画布标识符,表示在页面上的哪个 元素上进行绘制。
    120. var context = uni.createCanvasContext('firstCanvas')
    121. var width = '';
    122. const query = uni.createSelectorQuery();
    123. //获取宽度
    124. query.select('#firstCanvas').fields({
    125. size: true
    126. }, (res) => {
    127. width = res.width;
    128. //获取到宽度进行绘制
    129. //绘制路径中的线条。
    130. context.setStrokeStyle("#aaaaff")
    131. // 设置线条的宽度为2个像素。
    132. context.setLineWidth(2)
    133. // 绘制横线
    134. context.beginPath(); // 开始路径绘制
    135. context.moveTo(width / 2, 0); // 将起点移动到 (0, 100)
    136. context.lineTo(width / 2, 50);
    137. context.stroke(); // 绘制线条
    138. var x1 = width / 4; // 第一个竖线的起点 x 坐标
    139. var y1 = 50; // 第一个竖线的起点 y 坐标
    140. var y2 = 30; // 短竖线的高度
    141. var horizontalLength = width / 2; // 横线的长度
    142. context.beginPath();
    143. context.moveTo(x1, y1 + y2); // 移动到第一个短竖线的起点
    144. context.lineTo(x1, y1); // 绘制第一个短竖线
    145. context.moveTo(x1 + horizontalLength, y1 + y2); // 移动到横线的右端下方
    146. context.lineTo(x1 + horizontalLength, y1); // 绘制第二个短竖线
    147. context.moveTo(x1, y1); // 移动到横线的左端下方
    148. context.lineTo(x1 + horizontalLength, y1); // 绘制横线/* */
    149. context.stroke(); // 绘制线条
    150. // 将之前的绘图操作渲染到画布上。
    151. context.draw()
    152. }).exec();
    153. //获取父元素宽度
    154. query.select('.item3_top_text_position').fields({
    155. size: true
    156. }, (res) => {
    157. const parentWidth = res.width;
    158. // console.log("父元素的宽度:" + parentWidth)
    159. var marginLeft = parentWidth / 4;
    160. this.marginLeft1 = marginLeft;
    161. this.marginLeft2 = '-' + marginLeft;
    162. }).exec();
    163. },
    164. methods: {
    165. }
    166. }
    167. script>
    168. <style lang="scss">
    169. page {
    170. background-color: #f3f4f6;
    171. }
    172. .text_center {
    173. display: flex;
    174. justify-content: center;
    175. }
    176. /* 总体样式 */
    177. .content_position {
    178. width: 100%;
    179. display: flex;
    180. align-items: center;
    181. justify-content: center;
    182. }
    183. .content {
    184. width: 90%;
    185. padding: 2% 0;
    186. }
    187. /* SN 样式 */
    188. .SN {
    189. color: #6b6c6e;
    190. padding: 2%;
    191. display: flex;
    192. // border:1px solid black;
    193. justify-content: space-between;
    194. /* 将左右视图分散对齐 */
    195. .SN_input {
    196. display: flex;
    197. // border:1px solid black;
    198. input {
    199. border-bottom: 1px solid #a3a4a6;
    200. width: 200rpx;
    201. }
    202. image {
    203. width: 48rpx;
    204. height: 48rpx;
    205. }
    206. }
    207. }
    208. // 行1样式
    209. .line1 {
    210. // border: 1px solid black;
    211. background-color: #fff;
    212. border-radius: 15px;
    213. margin: 3% 0;
    214. padding: 3% 0;
    215. }
    216. .line1_item3 {
    217. // 顶部样式
    218. .item3_top {
    219. // position:relative;s
    220. }
    221. //第一行标题
    222. //标题位置
    223. .item3_top_title_position {
    224. // border: 1px solid black;
    225. display: flex;
    226. align-items: center;
    227. justify-content: center;
    228. }
    229. //标题内容
    230. .item3_top_title {
    231. width: 160rpx;
    232. height: 160rpx;
    233. border-radius: 50%;
    234. background-color: #edf1fc;
    235. color: #8196ec;
    236. font-size: 120%;
    237. font-weight: bold;
    238. display: flex;
    239. align-items: center;
    240. justify-content: center;
    241. // border:1px solid black
    242. }
    243. //画布位置
    244. .item3_top_canvas_position {
    245. // border: 1px solid black;
    246. display: flex;
    247. align-items: center;
    248. justify-content: center;
    249. }
    250. //文本位置
    251. .item3_top_text_position {
    252. // border: 1px solid black;
    253. position: relative;
    254. height: 400rpx;
    255. }
    256. .top_text1 {
    257. position: absolute;
    258. left: 50%;
    259. transform: translateX(-50%);
    260. margin-left: -85px;
    261. // border: 1px solid black;
    262. width: 220rpx;
    263. height: 350rpx;
    264. background-color: #edf1fc;
    265. border-radius: 10px;
    266. display: flex;
    267. align-items: center;
    268. justify-content: center;
    269. }
    270. .top_text2 {
    271. position: absolute;
    272. left: 50%;
    273. transform: translateX(-50%);
    274. margin-left: 85px;
    275. // border: 1px solid black;
    276. width: 220rpx;
    277. height: 350rpx;
    278. background-color: #edf1fc;
    279. border-radius: 10px;
    280. display: flex;
    281. align-items: center;
    282. justify-content: center;
    283. }
    284. .circle {
    285. width: 140rpx;
    286. height: 140rpx;
    287. border-radius: 50%;
    288. font-size: 95%;
    289. display: flex;
    290. align-items: center;
    291. justify-content: center;
    292. background-color: #e8e8e8;
    293. }
    294. // 底部样式
    295. .item3_bottom {
    296. display: flex;
    297. justify-content: space-between; //分散排列
    298. // border: 1px solid black;
    299. }
    300. //两列
    301. .line1_item3_block2 {
    302. display: flex;
    303. text-align: center;
    304. margin: 0 5%; //增加底部两个模块之间的距离
    305. }
    306. //两个小块样式
    307. .block2_input {
    308. margin: 5% 0;
    309. display: flex;
    310. }
    311. // 底部两小块标题
    312. .block2_input_title {
    313. width: 30%;
    314. display: flex;
    315. align-items: center; //竖直居中
    316. justify-content: center; //水平居中
    317. // border: 1px solid black;
    318. }
    319. .block2_input_data {
    320. width: 70%;
    321. }
    322. .block2_input_data input {
    323. margin: 5% 0;
    324. border: 1px solid #808080;
    325. border-radius: 20px;
    326. padding: 2% 5%;
    327. color: #737373;
    328. text-align: right;
    329. }
    330. //绿环样式
    331. .green_ring_select {
    332. background-color: #6da54f;
    333. border-radius: 20px;
    334. color: #fff;
    335. font-size: 95%;
    336. display: flex;
    337. align-items: center;
    338. padding: 1% 0;
    339. margin: 5% 0;
    340. // width:200rpx;
    341. view:nth-child(1) {
    342. width: 60%;
    343. display: flex;
    344. justify-content: center;
    345. align-items: center;
    346. }
    347. view:nth-child(2) {
    348. width: 40%;
    349. display: flex;
    350. justify-content: center;
    351. align-items: center;
    352. image {
    353. width: 30rpx;
    354. height: 30rpx;
    355. }
    356. }
    357. }
    358. }
    359. style>

  • 相关阅读:
    【Halcon图像拟合圆】
    麒麟服务器系统挂载磁盘
    QSystemTrayIcon——实现系统托盘
    C#程序随系统启动例子 - 开源研究系列文章
    【Echarts多种曲线实例】Echarts多条曲线不同时间轴对比(附代码)
    高德地图JSAPI 2.0使用Java代码代替Nginx进行反向代理产生CORS跨域
    设计模式的一些笔记(个人见解参杂各个模式的用途,未完待续0907)
    Mysql高可用
    MySQL 深度分页性能急剧下降,该如何优化?
    TypeReference使用笔记
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/133345919