-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add useWatermark hook and add useWatermark demo
- Loading branch information
1 parent
e4b7a76
commit d3fbd3a
Showing
10 changed files
with
432 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const domSymbol = Symbol('watermark-dom') | ||
|
||
export function useWatermark(appendEl: HTMLElement | null = document.body) { | ||
let func: Fn = () => {} | ||
const id = domSymbol.toString() | ||
const clear = () => { | ||
const domId = document.getElementById(id) | ||
if (domId) { | ||
const el = appendEl | ||
el && el.removeChild(domId) | ||
} | ||
window.removeEventListener('resize', func) | ||
} | ||
const createWatermark = (str: string) => { | ||
clear() | ||
|
||
const can = document.createElement('canvas') | ||
can.width = 300 | ||
can.height = 240 | ||
|
||
const cans = can.getContext('2d') | ||
if (cans) { | ||
cans.rotate((-20 * Math.PI) / 120) | ||
cans.font = '15px Vedana' | ||
cans.fillStyle = 'rgba(0, 0, 0, 0.15)' | ||
cans.textAlign = 'left' | ||
cans.textBaseline = 'middle' | ||
cans.fillText(str, can.width / 20, can.height) | ||
} | ||
|
||
const div = document.createElement('div') | ||
div.id = id | ||
div.style.pointerEvents = 'none' | ||
div.style.top = '0px' | ||
div.style.left = '0px' | ||
div.style.position = 'absolute' | ||
div.style.zIndex = '100000000' | ||
div.style.width = document.documentElement.clientWidth + 'px' | ||
div.style.height = document.documentElement.clientHeight + 'px' | ||
div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat' | ||
const el = appendEl | ||
el && el.appendChild(div) | ||
return id | ||
} | ||
|
||
function setWatermark(str: string) { | ||
createWatermark(str) | ||
func = () => { | ||
createWatermark(str) | ||
} | ||
window.addEventListener('resize', func) | ||
} | ||
|
||
return { setWatermark, clear } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<script setup lang="ts"> | ||
import { ContentWrap } from '@/components/ContentWrap' | ||
import { useI18n } from '@/hooks/web/useI18n' | ||
import { CountTo } from '@/components/CountTo' | ||
import { ElRow, ElCol, ElInputNumber, ElInput, ElButton } from 'element-plus' | ||
import { ref, unref } from 'vue' | ||
const { t } = useI18n() | ||
const countRef = ref<ComponentRef<typeof CountTo>>() | ||
const startVal = ref(0) | ||
const endVal = ref(1314512) | ||
const duration = ref(3000) | ||
const decimals = ref(0) | ||
const separator = ref(',') | ||
const prefix = ref('¥ ') | ||
const suffix = ref(' rmb') | ||
const autoplay = ref(false) | ||
const start = () => { | ||
unref(countRef)?.start() | ||
} | ||
const pauseResume = () => { | ||
unref(countRef)?.pauseResume() | ||
} | ||
</script> | ||
|
||
<template> | ||
<ContentWrap :title="t('countToDemo.countTo')" :message="t('countToDemo.countToDes')"> | ||
<div class="text-center mb-40px"> | ||
<CountTo | ||
ref="countRef" | ||
:start-val="startVal" | ||
:end-val="endVal" | ||
:duration="duration" | ||
:decimals="decimals" | ||
:separator="separator" | ||
:prefix="prefix" | ||
:suffix="suffix" | ||
:autoplay="autoplay" | ||
class="text-30px font-bold text-[var(--el-color-primary)]" | ||
/> | ||
</div> | ||
<ElRow :gutter="20" justify="space-between"> | ||
<ElCol :xl="8" :lg="8" :md="12" :sm="24" :xs="24"> | ||
<div class="flex mb-20px items-center"> | ||
<span class="min-w-90px text-right">{{ t('countToDemo.startVal') }}:</span> | ||
<ElInputNumber v-model="startVal" :min="0" /> | ||
</div> | ||
</ElCol> | ||
<ElCol :xl="8" :lg="8" :md="12" :sm="24" :xs="24"> | ||
<div class="flex mb-20px items-center"> | ||
<span class="min-w-90px text-right">{{ t('countToDemo.endVal') }}:</span> | ||
<ElInputNumber v-model="endVal" :min="1" /> | ||
</div> | ||
</ElCol> | ||
<ElCol :xl="8" :lg="8" :md="12" :sm="24" :xs="24"> | ||
<div class="flex mb-20px items-center"> | ||
<span class="min-w-90px text-right">{{ t('countToDemo.duration') }}:</span> | ||
<ElInputNumber v-model="duration" :min="1000" /> | ||
</div> | ||
</ElCol> | ||
<ElCol :xl="8" :lg="8" :md="12" :sm="24" :xs="24"> | ||
<div class="flex mb-20px items-center"> | ||
<span class="min-w-90px text-right">{{ t('countToDemo.separator') }}:</span> | ||
<ElInput v-model="separator" /> | ||
</div> | ||
</ElCol> | ||
<ElCol :xl="8" :lg="8" :md="12" :sm="24" :xs="24"> | ||
<div class="flex mb-20px items-center"> | ||
<span class="min-w-90px text-right">{{ t('countToDemo.prefix') }}:</span> | ||
<ElInput v-model="prefix" /> | ||
</div> | ||
</ElCol> | ||
<ElCol :xl="8" :lg="8" :md="12" :sm="24" :xs="24"> | ||
<div class="flex mb-20px items-center"> | ||
<span class="min-w-90px text-right">{{ t('countToDemo.suffix') }}:</span> | ||
<ElInput v-model="suffix" /> | ||
</div> | ||
</ElCol> | ||
<ElCol :span="24"> | ||
<div class="text-center"> | ||
<ElButton type="primary" @click="start">{{ t('countToDemo.start') }}</ElButton> | ||
<ElButton @click="pauseResume"> | ||
{{ t('countToDemo.pause') }}/{{ t('countToDemo.resume') }} | ||
</ElButton> | ||
</div> | ||
</ElCol> | ||
</ElRow> | ||
</ContentWrap> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<script setup lang="ts"> | ||
import { ContentWrap } from '@/components/ContentWrap' | ||
import { useI18n } from '@/hooks/web/useI18n' | ||
import { pieOptions, barOptions, lineOptions, wordOptions } from '@/views/Dashboard/echarts-data' | ||
import { Echart } from '@/components/Echart' | ||
import { ElRow, ElCol, ElCard } from 'element-plus' | ||
const { t } = useI18n() | ||
</script> | ||
|
||
<template> | ||
<ContentWrap :title="t('echartDemo.echart')" :message="t('echartDemo.echartDes')"> | ||
<ElRow :gutter="20" justify="space-between"> | ||
<ElCol :xl="10" :lg="10" :md="24" :sm="24" :xs="24"> | ||
<ElCard shadow="hover" class="mb-20px"> | ||
<Echart :options="pieOptions" :height="300" /> | ||
</ElCard> | ||
</ElCol> | ||
<ElCol :xl="14" :lg="14" :md="24" :sm="24" :xs="24"> | ||
<ElCard shadow="hover" class="mb-20px"> | ||
<Echart :options="barOptions" :height="300" /> | ||
</ElCard> | ||
</ElCol> | ||
<ElCol :span="24"> | ||
<ElCard shadow="hover" class="mb-20px"> | ||
<Echart :options="lineOptions" :height="350" /> | ||
</ElCard> | ||
</ElCol> | ||
<ElCol :span="24"> | ||
<ElCard shadow="hover" class="mb-20px"> | ||
<Echart :options="wordOptions" :height="300" /> | ||
</ElCard> | ||
</ElCol> | ||
</ElRow> | ||
</ContentWrap> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<script setup lang="ts"> | ||
import { ContentWrap } from '@/components/ContentWrap' | ||
import { useI18n } from '@/hooks/web/useI18n' | ||
import { ElButton } from 'element-plus' | ||
import { useWatermark } from '@/hooks/web/useWatermark' | ||
import { computed, onBeforeUnmount } from 'vue' | ||
import { useAppStore } from '@/store/modules/app' | ||
const appStore = useAppStore() | ||
const title = computed(() => appStore.getTitle) | ||
const { setWatermark, clear } = useWatermark() | ||
const { t } = useI18n() | ||
onBeforeUnmount(() => { | ||
clear() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<ContentWrap :title="t('watermarkDemo.watermark')"> | ||
<ElButton type="primary" @click="setWatermark(title)"> | ||
{{ t('watermarkDemo.createdWatermark') }} | ||
</ElButton> | ||
<ElButton type="danger" @click="clear">{{ t('watermarkDemo.clearWatermark') }}</ElButton> | ||
<ElButton type="warning" @click="setWatermark(`${title}-new`)"> | ||
{{ t('watermarkDemo.resetWatermark') }} | ||
</ElButton> | ||
</ContentWrap> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.