MATLAB | 一起来绘制一款元旦特别款烟花叭~

2023-12-31 11:01:49

新的一年就要到了,祝大家元旦快乐,新的一年里顺风顺水顺财神,文章投的都中,奖金基金kuku申请成功,今天带来一款完成度比较高的烟花代码,带字幕特效,为新的一年接风洗尘~:
在这里插入图片描述

文字都是可以换的,也可以更多句话,只需要修改STR元胞数组内的内容:

if nargin<1
    STR={'MATLABer','元旦快乐'};
end

改点字过年的时候还可以接着用哈哈哈哈。


完整代码

完整代码有点长,日后该代码如果有更新可以去我的gitee仓库进行查看。

function subtitleFireworks(STR)
% Copyright (c) 2023, Zhaoxu Liu / slandarer
if nargin<1
    STR={'MATLABer','元旦快乐'};
end

fwSound=load('splat.mat');
% 循环构建文段矩阵
STRMX{length(STR)}=[];
STRMY{length(STR)}=[];
for i=1:length(STR)
    string=STR{i};
    CaptionMat=zeros(25*length(string),25);
    for j=1:length(string)
        CaptionMat(25*(j-1)+1:25*j,:)=getWordMatrix(string(j));
    end
    [XMesh,YMesh]=find(CaptionMat~=0);
    sizeSTRM=size(CaptionMat);
    STRMX{i}=(XMesh-sizeSTRM(1)/2)./12.5;
    STRMY{i}=(YMesh-sizeSTRM(2)/2)./12.5;
end

fig=figure('units','normalized','position',[.1,.1,.5,.8],...
    'UserData',[98,121,32,115,108,97,110,100,97,114,101,114]);
axes('parent',fig,'NextPlot','add','Color',[0,0,0],...
    'DataAspectRatio',[1,1,1],'XLim',[-100,100],'YLim',[0,200],'Position',[0,0,1,1]);
disp(char(fig.UserData))
% 绘制一些静态装饰
if true
starX=rand(1,50).*200-100;starY=rand(1,50).*110+90;
scatter(starX,starY,40,'white','filled','MarkerEdgeColor','none','MarkerFaceAlpha',.1)
scatter(starX,starY,4,'white','filled','MarkerEdgeColor','none','MarkerFaceAlpha',.9)
for i=1:20
    skyscraper(5+i*10-110+2,rand([1,1])*20+30,[47,46,70]./255./4,[253,243,177]./255./4)
    skyscraper(5+i*10-110,rand([1,1])*25+15,[47,46,70]./255,[253,243,177]./255)
end
end

%% ========================================================================
% 主循环
for i=length(STR)+1:99
    indSTRM=mod(i-1,length(STR)+2)+1;
    if length(STR)+1==indSTRM||length(STR)+2==indSTRM
        drawFireworks()
    else
        drawWordMatrix(indSTRM)
    end
end
%% ========================================================================
% 一些静态绘制函数
    function skyscraper(X,Y,C1,C2)
        W=4.6;H=50;
        fill([X-W,X+W,X+W,X-W],[Y,Y,Y+H,Y+H]-H,C1)
        [XW,YW]=meshgrid([-1.9,1.9],linspace(.5,50-1.5,15));
        CMat=C2.*(rand([length(XW(:)),1])>.5);
        scatter(XW(:)+X,YW(:)+Y-H-1.5,35,'filled','CData',CMat,'Marker','s','MarkerEdgeColor','none')
    end
%% ========================================================================
% 一些动态绘制函数
    function drawWordMatrix(N)
        drawBullet(0,140,[1,1,1]);
        wordHdl1=scatter(STRMX{N},STRMY{N}+140,15,'filled','CData',[1,1,1],'Marker','s','MarkerEdgeColor','none','MarkerFaceAlpha',.5);
        wordHdl2=scatter(STRMX{N},STRMY{N}+140,15,'filled','CData',[1,1,1],'Marker','s','MarkerEdgeColor','none');
        set(gca,'Color',[1,1,1]./8);
        for ii=linspace(2,30,30)
           wordHdl1.XData=STRMX{N}.*(ii-1);
           wordHdl1.YData=STRMY{N}.*(ii-1)+140;
           wordHdl2.XData=STRMX{N}.*ii;
           wordHdl2.YData=STRMY{N}.*ii+140;
           set(gca,'Color',[1,1,1]./8*(1-ii/30));
           drawnow;pause(.05)
        end
        delete(wordHdl1)
        delete(wordHdl2)
    end
    function drawBullet(X,Y,C)
        YY=linspace(0,-12,20);
        XX=sin(YY)./8;XX=XX-XX(1)+X;
        Alp=linspace(1,.01,20);
        Siz=linspace(45,8,20);
        bulletHdl=scatter(XX,YY,'filled','CData',C,'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        sound(fwSound.y(1:7200),fwSound.Fs/1.2)
        for ii=linspace(0,Y,30)
            YY=linspace(0,-12,20)+ii;
            XX=sin(linspace(0,-12,20)+ii/2)./8;XX=XX-XX(1)+X;
            bulletHdl.XData=XX;
            bulletHdl.YData=YY;
            drawnow;pause(.06)
        end
        delete(bulletHdl)
        sound(fwSound.y(7201:10001),fwSound.Fs)
    end
    function drawFireworks(~,~)
        % Copyright (c) 2023, Zhaoxu Liu / slandarer
        YY=linspace(0,-12,20);
        XX=sin(YY)./8;XX=XX-XX(1);
        X=rand([3,1]).*200-100;
        Y=rand([3,1]).*80+90;
        C=rand(3,3)./2+.5;
        Alp=linspace(1,.01,20);
        Siz=linspace(45,8,20);
        bulletHdl1=scatter(XX+X(1),YY,'filled','CData',C(1,:),'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        bulletHdl2=scatter(XX+X(2),YY,'filled','CData',C(2,:),'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        bulletHdl3=scatter(XX+X(3),YY,'filled','CData',C(3,:),'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        sound(fwSound.y(1:7200),fwSound.Fs/1.2)
        for ii=linspace(0,1,30)
            YY1=linspace(0,-12,20)+ii*Y(1);XX1=sin(linspace(0,-12,20)+ii*Y(1)/2)./8;XX1=XX1-XX1(1)+X(1);
            YY2=linspace(0,-12,20)+ii*Y(2);XX2=sin(linspace(0,-12,20)+ii*Y(2)/2)./8;XX2=XX2-XX2(1)+X(2);
            YY3=linspace(0,-12,20)+ii*Y(3);XX3=sin(linspace(0,-12,20)+ii*Y(3)/2)./8;XX3=XX3-XX3(1)+X(3);
            bulletHdl1.XData=XX1;bulletHdl1.YData=YY1;
            bulletHdl2.XData=XX2;bulletHdl2.YData=YY2;
            bulletHdl3.XData=XX3;bulletHdl3.YData=YY3;
            drawnow;pause(.06)
        end
        delete(bulletHdl1);delete(bulletHdl2);delete(bulletHdl3)
        sound(fwSound.y(7201:10001),fwSound.Fs)

        T=rand([1,300]).*2.*pi;R=rand([1,300]).*1+1;
        XF=cos(T).*R;YF=sin(T).*R;K=linspace(1,2,10).';
        Alp=linspace(.01,.7,10).'.*ones(1,100);Alp=Alp(:);
        Siz=linspace(4,40,10).'.*ones(1,100);Siz=Siz(:);
        R2=(XF.*K).^2+(YF.*K).^2;
        XXF=XF.*K;YYF=YF.*K-R2./90;
        XXF1=XXF(:,1:100);XXF1=XXF1(:);YYF1=YYF(:,1:100);YYF1=YYF1(:);
        XXF2=XXF(:,101:200);XXF2=XXF2(:);YYF2=YYF(:,101:200);YYF2=YYF2(:);
        XXF3=XXF(:,201:300);XXF3=XXF3(:);YYF3=YYF(:,201:300);YYF3=YYF3(:);
        fireworksHdl1=scatter(XXF1+X(1),YYF1+Y(1),'filled','CData',C(1,:),'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        fireworksHdl2=scatter(XXF2+X(2),YYF2+Y(2),'filled','CData',C(2,:),'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        fireworksHdl3=scatter(XXF3+X(3),YYF3+Y(3),'filled','CData',C(3,:),'AlphaData',Alp,'SizeData',Siz,'MarkerFaceAlpha','flat');
        set(gca,'Color',(C(1,:)+C(1,:)+C(1,:))./30);
        for ii=linspace(2,20,30)
            XXF(1:end-1,:)=XXF(2:end,:);XXF(end,:)=XF.*ii;
            R2=(XF.*ii).^2+(YF.*ii).^2;
            YYF(1:end-1,:)=YYF(2:end,:);YYF(end,:)=YF*ii-R2./90;
            XXF1=XXF(:,1:100);XXF1=XXF1(:);YYF1=YYF(:,1:100);YYF1=YYF1(:);
            XXF2=XXF(:,101:200);XXF2=XXF2(:);YYF2=YYF(:,101:200);YYF2=YYF2(:);
            XXF3=XXF(:,201:300);XXF3=XXF3(:);YYF3=YYF(:,201:300);YYF3=YYF3(:);
            fireworksHdl1.XData=XXF1+X(1);fireworksHdl1.YData=YYF1+Y(1);
            fireworksHdl2.XData=XXF2+X(2);fireworksHdl2.YData=YYF2+Y(2);
            fireworksHdl3.XData=XXF3+X(3);fireworksHdl3.YData=YYF3+Y(3);
            set(gca,'Color',(C(1,:)+C(1,:)+C(1,:))./30*(1-ii/20));
            drawnow;pause(.02)
        end
        delete(fireworksHdl1);delete(fireworksHdl2);delete(fireworksHdl3)
    end
%% ========================================================================
    % 文字矩阵生成函数
    function wordMatrix=getWordMatrix(CHAR)
        % Copyright (c) 2023, Zhaoxu Liu / slandarer
        figGWM=figure('units','pixels','position',[20 20 160 160],...
            'Numbertitle','off','Color',[1 1 1],'resize','off',...
            'visible','off','menubar','none');
        axGWM=axes('Units','pixels','parent',figGWM,'Color',[1 1 1],...
            'Position',[0 0 160 160],'XLim',[0 16],'YLim',[0 16],...
            'XColor',[1 1 1],'YColor',[1 1 1],'NextPlot','add');
        text(axGWM,8,8.5,CHAR,'HorizontalAlignment','center','FontSize',120)
        saveas(figGWM,['.\',CHAR,'.png']);pic=imread(['.\',CHAR,'.png']);
        delete(['.\',CHAR,'.png']);delete(axGWM);set(figGWM,'Visible','on');close all
        [rowMax,colMax,~]=size(pic);
        picData=pic(:,:,1)<125;
        wordMatrix=zeros(25,25);
        % 统计每个像素内点数确定黑色还是白色
        for ii=1:25
            rowLim=round([ii-1,ii]./25.*rowMax);
            rowLim(rowLim==0)=1;
            for jj=1:25
                colLim=round([jj-1,jj]./25.*colMax);
                colLim(colLim==0)=1;
                wordMatrix(ii,jj)=sum(sum(picData(rowLim(1):rowLim(2),colLim(1):colLim(2))));
            end
        end
        wordMatrix(wordMatrix<10)=0;
        wordMatrix=wordMatrix';
        wordMatrix=wordMatrix(:,end:-1:1);
    end
end

文章来源:https://blog.csdn.net/slandarer/article/details/135313343
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。