sca;
close all;
clear;
PsychDefaultSetup(2);
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, black);
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
ifi = Screen('GetFlipInterval', window);
[xCenter, yCenter] = RectCenter(windowRect);
baseRect = [0 0 200 200];
amplitude = screenXpixels * 0.25;
frequency = 0.2;
angFreq = 2 * pi * frequency;
startPhase = 0;
time = 0;
vbl = Screen('Flip', window);
waitframes = 1;
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);
while ~KbCheck
xpos = amplitude * sin(angFreq * time + startPhase);
colorMultiplier = abs(xpos) / amplitude;
squareXpos = xCenter + xpos;
centeredRect = CenterRectOnPointd(baseRect, squareXpos, yCenter);
Screen('FillRect', window, [1 - colorMultiplier 0 colorMultiplier],...
centeredRect);
vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
time = time + ifi;
end
sca;