轮播图使用插件:nuka-carousel
参考示例:腾讯视频
class AutoImg extends PureComponent {
// ....
setImg(imgUrl) {
const { width, height } = this.props
const img = new Image()
img.src = imgUrl
img.crossOrigin = '' // 处理图片跨域
img.onload = function () {
const ctx = this.cv.getContext('2d')
ctx.drawImage(img, 0, 0, width, height)
// 获取图片背景色
const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data
this.inner.style.backgroundColor = `rgba(${r}, ${g}, ${b}, ${a})`
}
}
render() {
const { width, height } = this.props
return (
<div ref={inner => this.inner}>
<canvas
style={{
display: 'block',
margin: '0 auto',
}}
width=`${width}px`
height=`${height}px`
ref={cv => this.cv = cv}>
</canvas>
</div>
)
}
}
参考:createImageData()、getImageData() 以及 putImageData() 方法,以获得更多关于 ImageData 对象的知识。