解析已有数据源,自定义渲染日历

效果图:
在这里插入图片描述
模拟数据源:

// date.js
// 模拟数据源,实际开发中可从后端接口中获取
export const days = [
  { daysDate: '2024-07-01', today: 1 },
  { daysDate: '2024-07-02', today: 2 },
  { daysDate: '2024-07-03', today: 3 },
  { daysDate: '2024-07-04', today: 4 },
  { daysDate: '2024-07-05', today: 5 },
  { daysDate: '2024-07-06', today: 6 },
  { daysDate: '2024-07-07', today: 7 },
  { daysDate: '2024-07-08', today: 1 },
  { daysDate: '2024-07-09', today: 2 },
  { daysDate: '2024-07-10', today: 3 },
  { daysDate: '2024-07-11', today: 4 },
  { daysDate: '2024-07-12', today: 5 },
  { daysDate: '2024-07-13', today: 6 },
  { daysDate: '2024-07-14', today: 7 },
  { daysDate: '2024-07-15', today: 1 },
  { daysDate: '2024-07-16', today: 2 },
  { daysDate: '2024-07-17', today: 3 },
  { daysDate: '2024-07-18', today: 4 },
  { daysDate: '2024-07-19', today: 5 },
  { daysDate: '2024-07-20', today: 6 },
  { daysDate: '2024-07-21', today: 7 },
  { daysDate: '2024-07-22', today: 1 },
  { daysDate: '2024-07-23', today: 2 },
  { daysDate: '2024-07-24', today: 3 },
  { daysDate: '2024-07-25', today: 4 },
  { daysDate: '2024-07-26', today: 5 },
  { daysDate: '2024-07-27', today: 6 },
  { daysDate: '2024-07-28', today: 7 },
  { daysDate: '2024-07-29', today: 1 },
  { daysDate: '2024-07-30', today: 2 },
  { daysDate: '2024-07-31', today: 3 }
]

vue 转换代码:

<template>
  <div class="calendar-wrapper">
    <div class="row weeks">
      <div class="col" v-for="(item, index) in ['日', '一', '二', '三', '四', '五', '六']" :key="index">{{ item }}</div>
    </div>
    <div class="date-wrapper">
      <div class="row" v-for="(row, rowIndex) in month" :key="rowIndex">
        <div
          v-for="(col, colIndex) in row"
          :key="colIndex"
          :class="['col', { 'is-weekend': col.today % 7 === 6 || col.today % 7 === 0, 'is-today': showDate(col) === '今天' }]"
        >
          {{ showDate(col) }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { days } from './date'

let month = ref([])
let week = []
// 将日期一维数组按照每周转换成二维数组
days.forEach(i => {
  if (i.today % 7 === 0 && week.length) {
    month.value.push(week)
    week = []
  }
  week.push(i)
})

// 最后一周的长度如果小于 7 就在数组后面进行填充,将长度填充到 7
if (week.length < 7) {
  week = week.concat(new Array(7 - week.length).fill(''))
}
month.value.push(week)

// 第一周的长度如果小于 7 就在数组前面进行填充,将长度填充到 7
const week1 = month.value[0]
if (week1.length < 7) {
  month.value[0] = new Array(7 - week1.length).fill('').concat(week1)
}

// 对页面中展示的日期进行格式化处理
const showDate = computed(() => {
  return i => {
    if (i && i.daysDate) {
      if (i.daysDate.substr(-2) * 1 === new Date().getDate()) {
        return '今天'
      }
      return i.daysDate.substr(-2) * 1
    }
  }
})
</script>

<style>
.row {
  display: flex;
}
.col {
  width: 40px;
  height: 30px;
  text-align: center;
}
.weeks .col {
  font-weight: bold;
}
.is-weekend {
  color: blue;
}
.is-today {
  color: red;
}
</style>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Cesium中,您可以使用`CustomDataSource`和`CustomRenderPrimitive`来自定义渲染代码。 `CustomDataSource`是一种特殊类型的`DataSource`,它允许您在场景中添加自定义的实体和几何图形。您可以使用`CustomRenderPrimitive`在场景中添加自定义渲染图元,每个渲染图元都可以实现自己的渲染逻辑。 以下是一个简单的示例,演示如何使用`CustomDataSource`和`CustomRenderPrimitive`来自定义渲染代码: ```javascript // 创建Cesium Viewer var viewer = new Cesium.Viewer('cesiumContainer'); // 创建自定义数据源 var customDataSource = new Cesium.CustomDataSource('custom'); // 添加自定义实体 var entity = customDataSource.entities.add({ position: Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883), billboard: { image: 'path/to/image.png', scale: 2.0 } }); viewer.dataSources.add(customDataSource); // 创建自定义渲染图元 var customPrimitive = new Cesium.CustomRenderPrimitive({ geometryInstances: new Cesium.GeometryInstance({ geometry: new Cesium.RectangleGeometry({ rectangle: Cesium.Rectangle.fromDegrees(-75.0, 35.0, -125.0, 45.0) }), attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED) } }), fragmentShaderSource: 'void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }', vertexShaderSource: 'attribute vec3 position3DHigh;\ attribute vec3 position3DLow;\ attribute vec4 color;\ varying vec4 v_color;\ void main() {\ vec4 position = czm_computePosition();\ gl_Position = czm_viewProjection * position;\ v_color = color;\ }', renderState: { blending: Cesium.BlendingState.DISABLED, depthTest: { enabled: true, func: Cesium.DepthFunction.LESS }, depthMask: true, polygonOffset: { enabled: false, factor: 0.0, units: 0.0 } } }); viewer.scene.primitives.add(customPrimitive); ``` 在上面的代码中,我们首先创建了一个Cesium Viewer,并创建了一个自定义数据源。然后,我们向自定义数据源中添加了一个自定义实体,并将其添加到场景中。接下来,我们创建了一个自定义渲染图元,并将其添加到场景中。在自定义渲染图元中,我们使用了自定义的顶点着色器和片段着色器,并设置了渲染状态。 请注意,上面的示例仅用于演示目的。实际应用中,您可能需要更复杂的渲染逻辑和着色器代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shifeng~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值