w 和 h 分别指窗口的宽和高
- componentDidMount(){
- var c=document.getElementById("canvas");
- var ctx=c.getContext("2d");
- ctx.fillStyle = "#fff";
- ctx.fillRect(0, 0, 100,50);
- ctx.font="50px Arial";
- ctx.textBaseline = "top";
- ctx.fillStyle = "red";
- ctx.fillText("HELLO",0,0);
- var imgData=ctx.getImageData(0,0,300,50).data;
- let point=[] , x=[]
- for(let i=0;i<imgData.length;i++){
- for(let j=i*1200;j<(i+1)*1200;j+=4){
- if(imgData[j]==255 && imgData[j+1]==0 && imgData[j+2]==0 ){
- x.push(j%1200)
- }
- }
- if(x.length){
- point.push(x)
- x=[]
- }
- }
- c.width=w
- c.height=h
- ctx.fillStyle='red'
- for(let i=0;i<point.length;i++){
- for(let j=0;j<point[i].length;j++){
- ctx.beginPath();
- ctx.arc(point[i][j],i*10+100,2,0,2*Math.PI);
- ctx.fill()
- }
- }
- }
web 前端开发学习 Q-q-u-n: 731771211, 分享学习的方法和需要注意的小细节, 不停更新最新的教程和学习方法 (详细的前端项目实战教学视频, PDF)
来源: http://www.jianshu.com/p/207db45089e0