<template>
	<view class="strockplay-redInner" :style="{'width':bjIconSize+'px','height':bjIconSize+'px'}">
		<view v-if="playStorkes" :class="['strock-play-box']" @click="playHanzi">
			<SvgIcon icon-class="hanzi-strock-play" class="strock-play-btn" />
		</view>
		<view class="character-target-box">
			<SvgIcon icon-class="hanzi-writer-bg" class="character-target-bg" :size="bjIconSize" />
			<view :id="targetView" class="character-target-div"></view>
		</view>
	</view>
</template>

<script>
	import HanziWriter from 'hanzi-writer';
	export default {
		name: 'Strockplayredline',
		components: {},
		props: {
			bookText: {
				type: String,
				default: '',
			},
			targetView: {
				type: String,
				default: '',
			},
			playStorkes: {
				type: Boolean,
				default: true,
			},
			strokeColor: {
				type: String,
				default: '',
			},
			bookStrokes: {
				type: Object,
				default: () => {},
			},
			bjIconSize: {
				type: Number,
				default: 64,
			}
		},
		data() {
			return {
				writer: null,
			};
		},
		computed: {},
		watch: {
			targetView: {
				handler(val, oldVal) {
					if (val !== oldVal) {
						this.$nextTick(() => {
							this.initHanziwrite();
						});
					}
				},
				deep: true,
			},
			// strokeColor: {
			// 	handler(val, oldVal) {
			// var that = this;
			// if (this.targetView.indexOf('con_') >= 0) {
			// 	if (this.writer) {
			// that.writer.updateColor('strokeColor', val);
			// 	}
			// }
			// 	},
			// 	immediate: true,
			// 	deep: true,
			// },
		},
		mounted() {
			this.$nextTick(() => {
				this.initHanziwrite();
			});
		},
		// 方法集合
		methods: {
			initHanziwrite() {
				let that = this;
				this.writer = HanziWriter.create(this.targetView, this.bookText, {
					charDataLoader(char, onComplete) {
						onComplete(that.bookStrokes);
					},
					width: this.bjIconSize,
					height: this.bjIconSize,
					padding: 5,
					showOutline: true, //是否显示轮廓
					strokeColor: this.strokeColor ? this.strokeColor : '#000', //笔画颜色
				});
			},
			playHanzi(event) {
				event.stopPropagation();
				this.writer.animateCharacter(); //动画
			},
		},
	};
</script>

<style lang="scss" scoped>
	.strockplay-redInner {
		position: relative;
		box-sizing: border-box;
	}

	.character-target-div {
		position: absolute;
		top: 0;
		left: 0;
		z-index: 11;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100%;
	}

	.character-target-box {
		position: relative;
		width: 100%;
		height: 100%;
	}

	.character-target-bg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		color: #676767;
	}

	.strock-play-box {
		position: absolute;
		top: 0;
		right: 0;
		z-index: 12;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 11px;
		height: 11px;
		color: red;
	}
</style>