Donald J. Trump vanquished his opponents in the Massachusetts Republican primary on Tuesday, his projected victory declared moments after the polls closed.

Nearly 20,000 Massachusetts voters dropped out of the Democratic Party in recent weeks and changed their affiliation to Republican — most likely to vote for Trump, according to Secretary of State William Galvin, a Democrat. Unenrolled voters can vote in either primary.

Galvin said in an interview that he had seen such shifts in previous primaries, including in 2000, when Democrats switched parties to vote for Sen. John McCain. But, he said, the movement this year was the most he has seen in such a short time frame. “It’s clearly all Trump-related,” he said.

The state has plenty of angry white, working-class voters who were drawn to Trump, much as they were drawn to Scott Brown in his successful Senate race in 2010. Another factor believed to be in Trump’s favor here was that he was the least overtly religious of the candidates; like the rest of New England, Massachusetts is among the least religious states.

Trump dominated nearly every demographic group, losing only among voters with postgraduate degrees, who supported Sen. Marco Rubio, according to exit polls by Edison Research.

Trump’s several visits to the state and outspoken admiration for New England Patriots quarterback Tom Brady probably did not hurt, nor did Brady’s reciprocal support.