轮播图使用插件:nuka-carousel
    参考示例:腾讯视频

    1. class AutoImg extends PureComponent {
    2. // ....
    3. setImg(imgUrl) {
    4. const { width, height } = this.props
    5. const img = new Image()
    6. img.src = imgUrl
    7. img.crossOrigin = '' // 处理图片跨域
    8. img.onload = function () {
    9. const ctx = this.cv.getContext('2d')
    10. ctx.drawImage(img, 0, 0, width, height)
    11. // 获取图片背景色
    12. const [r, g, b, a] = ctx.getImageData(0, 0, 1, 1).data
    13. this.inner.style.backgroundColor = `rgba(${r}, ${g}, ${b}, ${a})`
    14. }
    15. }
    16. render() {
    17. const { width, height } = this.props
    18. return (
    19. <div ref={inner => this.inner}>
    20. <canvas
    21. style={{
    22. display: 'block',
    23. margin: '0 auto',
    24. }}
    25. width=`${width}px`
    26. height=`${height}px`
    27. ref={cv => this.cv = cv}>
    28. </canvas>
    29. </div>
    30. )
    31. }
    32. }

    参考:createImageData()getImageData() 以及 putImageData() 方法,以获得更多关于 ImageData 对象的知识。